请帮我解决这个问题。我试图弄清楚如何避免尚未批准在我的平台上发帖的用户
这是我在控制器中的代码
// current authenticated user status
$userStatus = auth()->user()->status;
// if the current authenticated user status is not approved return false
if($userStatus != Constants::APPROVED){
return back()->with('error_message', 'not approved');
}
这是我的常量档案
class Constants{
const APPROVED = "approved";
const PENDING = "pending";
const REJECTED = "rejected";
最后我的迁移
$table->string('status')->default("Pending");
uj5u.com热心网友回复:
你撰写$table->string('status')->default("Pending");
并找到你的 const vars 你把它写成小写。那将是问题所在。请检查您在设定状态时是否还考虑小写和大写。
class Constants{
const APPROVED = "approved";
const PENDING = "pending";
const REJECTED = "rejected";
uj5u.com热心网友回复:
Laravel 已经为您确定了授权,您需要做的是创建一个名为 (eg. Status)的中间件档案
public function handle($request, Closure $next)
{
if ($request->user()->status !== 'APPROVED') {
return back()->with('error_message', 'not approved'); // do something if the user is not approved
}
return $next($request); // else just continue
}
现在在您的路线中添加中间件
Route::post('/post', [PostController::class, 'post'])->middleware(['approved']);
0 评论