微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我的中间件在 Laravel 上运行时出现“在 strin 上调用成员函数 send()”错误

如何解决当我的中间件在 Laravel 上运行时出现“在 strin 上调用成员函数 send()”错误

如果我的数据库为空,我想制作一个中间件将我的所有请求重定向到“仪表板”路由,但此错误出现在页面上。

public function handle(Request $request,Closure $next)
{
    
    $wallets = Wallet::get()->count();
    echo $wallets;
    if($wallets == 0){

        return route('dashboard');        
    }

    return $next($request);
}

错误

在字符串上调用成员函数 send()

我的路线:

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,]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。