ahmad
ahmad

Reputation: 11

"Call to a member function send() on strin" error appear when my middleware run on laravel

I want to make a middleware to redirect all my requests to 'dashboard' route if my database is empty, but this error appears on page.

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

        return route('dashboard');        
    }

    return $next($request);
}

Error:

Call to a member function send() on string

My routes:

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', [WalletController::class, '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}', [UpdateWalletController::class, 'addBalenceGet'])->name('addBalence');

    // show balences
    Route::get('/balenceDetails/{walletId}', [UpdateWalletController::class, 'balenceDetails']);

    // add balences
    Route::post('/addBalancePost/{walletId}', [UpdateWalletController::class, 'addBalencePost'])->name('addBalancePost');

    // delete wallet 
    Route::get('/deleteWallet/{walletId}', [UpdateWalletController::class, 'deleteWallet'])->name('deleteWallet');
});


require __DIR__.'/auth.php';

Upvotes: -1

Views: 694

Answers (1)

ahmad
ahmad

Reputation: 11

Oh i found the answer: I configed wrong kernel.php, i had added my middleware to

protected $middleware = [...]

But it just should add to:

protected $routeMiddleware = [
        'FirstWalletCheck' => \App\Http\Middleware\FirstWalletCheck::class,]

Upvotes: 1

Related Questions