請幫我解決這個問題。我試圖弄清楚如何避免尚未批準在我的平臺上發帖的用戶
這是我在控制器中的代碼
// 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']);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/407111.html
標籤:
