问题描述
如果我的数据库为空,我想制作一个中间件将我的所有请求重定向到“仪表板”路由,但此错误出现在页面上。
public function handle(Request $request,Closure $next)
{
$wallets = Wallet::get()->count();
echo $wallets;
if($wallets == 0){
return route('dashboard');
}
return $next($request);
}
错误:
我的路线:
Route::get('/',function () {
return view('home');
})->name('home');
// show dashboard
Route::get('/dashboard',[WalletController::class,'show'])->middleware(['auth'])->name('dashboard');
// make new wallet
Route::post('/newWallet','store'])->name('newWallet');
Route::middleware('FirstWalletCheck')->group(function() {
// show all wallets
Route::get('/wallets',[UpdateWalletController::class,'showWallets'])->name('wallets');
// add balences
Route::get('/addBalenceGet/{walletId}','addBalenceGet'])->name('addBalence');
// show balences
Route::get('/balenceDetails/{walletId}','balenceDetails']);
// add balences
Route::post('/addBalancePost/{walletId}','addBalencePost'])->name('addBalancePost');
// delete wallet
Route::get('/deleteWallet/{walletId}','deleteWallet'])->name('deleteWallet');
});
require __DIR__.'/auth.PHP';
解决方法
哦,我找到了答案: 我配置了错误的 kernel.php,我已将中间件添加到
protected $middleware = [...]
但它应该添加到:
protected $routeMiddleware = [
'FirstWalletCheck' => \App\Http\Middleware\FirstWalletCheck::class,]