我制作了一个中间件,如果用户不注册为提供者,则用户无法发布奖学金。它之前可以工作,但现在在向系统添加策略和软删除后抛出错误。我想知道我的部分哪里出了问题。
中间件/Scholarprovider.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class Scholarprovider
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $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);
}
}
学者请求.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Models\Scholarship;
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, \Illuminate\Contracts\Validation\ValidationRule|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',
];
}
}
奖学金政策.php
<?php
namespace App\Policies;
use App\Models\Scholarship;
use App\Models\User;
use Illuminate\Auth\Access\Response;
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(我添加了软删除)
<?php
namespace App\Http\Controllers;
use App\Models\ScholarshipApplication;
use Illuminate\Http\Request;
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.'
);
}
}
到目前为止,我尝试的是将奖学金请求链接到中间件,但似乎 Laravel 找不到控制器,导致无法创建新的奖学金。
在您的
ScholarshipRequest
文件中,您的命名空间是
namespace App\Http\Requests;
但是你的错误是
"App\Http\Request\ScholarshipRequest" does not exist
请求!=请求。
这是一个拼写错误问题。将使用
App\Http\Request\ScholarshipRequest
的部分更改为 App\Http\Requests\ScholarshipRequest