-1

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';
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
ahmad
  • 11
  • 5
  • I would suggest not using `echo` in your project. A) You likely won't be able to see it, and B) there are other methods to log information, like `\Log::info($wallets);` will log the count to `storage/laravel.log` file. That being said, I'm not sure where your error is coming from; can you include more of your error message? There should be a stacktrace associated with it that shows the file/line where this is happening. – Tim Lewis Jul 13 '21 at 14:01

1 Answers1

1

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,]
ahmad
  • 11
  • 5