I made a middleware where the users are not able to publish scholarships if the user doesn’t register as provider. It was working earlier but now it was throwing error after adding policy and soft deletes to the system. I am wondering where did I go wrong from my part.
Middleware/Scholarprovider.php
<?php
namespace AppHttpMiddleware;
use Closure;
use IlluminateHttpRequest;
use SymfonyComponentHttpFoundationResponse;
class Scholarprovider
{
/**
* Handle an incoming request.
*
* @param Closure(IlluminateHttpRequest): (SymfonyComponentHttpFoundationResponse) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (null === $request->user() || null === $request->user()->scholarprovider) {
return redirect()->route('scholarprovider.create')
->with('error', 'You need to register for Scholarprovider first!');
}
return $next($request);
}
}
Scholarrequest.php
<?php
namespace AppHttpRequests;
use IlluminateFoundationHttpFormRequest;
use AppModelsScholarship;
class ScholarshipRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, IlluminateContractsValidationValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'title' => 'required|string|max:225',
'description' => 'required|string|max:225',
'schoolname' => 'required|string|max:225',
'requirements' => 'required|string|max:225',
'location' => 'required|string|max:225',
'contactno' => 'required|string|max:225',
'category' => 'required|in:' . implode(',', Scholarship::$category),
'grants' => 'required|in:' . implode(',', Scholarship::$grants),
'grade_needed' => 'required|numeric|max:100',
'monetary_value' => 'required|numeric|min:1000',
];
}
}
ScholarshipPolicy.php
<?php
namespace AppPolicies;
use AppModelsScholarship;
use AppModelsUser;
use IlluminateAuthAccessResponse;
class ScholarshipPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(?User $user): bool
{
return true;
}
public function viewAnyScholarprovider(User $user): bool
{
return true;
}
/**
* Determine whether the user can view the model.
*/
public function view(?User $user, Scholarship $scholarship): bool
{
return true;
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return $user->scholarprovider !== null;
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, Scholarship $scholarship): bool | Response
{
if ($scholarship->scholarprovider->user_id !== $user->id) {
return false;
}
if ($scholarship->scholarshipApplications()->count() > 0) {
return Response::deny('Cannot change the Scholarship with applications');
}
return true;
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Scholarship $scholarship): bool
{
return false;
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Scholarship $scholarship): bool
{
return false; //
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Scholarship $scholarship): bool
{
return false;
}
public function apply(User $user, Scholarship $scholarship): bool
{
return !$scholarship->hasUserApplied($user);
}
}
MyScholarshipController.php (where I added soft delete)
<?php
namespace AppHttpControllers;
use AppModelsScholarshipApplication;
use IlluminateHttpRequest;
class MyScholarshipApplicationController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return view('my_scholarship_application.index',
[
'applications' => auth()->user()->scholarshipApplications()
->with([
'scholarship' => function ($query) {
$query->withCount('scholarshipApplications')
->withTrashed();
},
'scholarship.scholarprovider'
])
->latest()
->get()
]
);
}
public function destroy(ScholarshipApplication $myScholarshipApplication)
{
$myScholarshipApplication->delete();
return redirect()->back()->with(
'success',
'Scholarship Application Removed.'
);
}
}
What I tried so far is to link ScholarshipRequest to Middleware but it seems laravel couldn’t find the Controller resulting for it being unable to create new Scholarships.