Skip to content
Advertisement

Laravel not calling function inside if statement

In laravel 8 I’m trying to pass data to the view blade template. This is my code:

Route::get('/admin', function(){
    if (session()->has('username')) {
        return view('admin', ["privatecars"=>[privatecar::class, 'list'], "businesscars"=>[businesscar::class, 'list']]);
    } else {
        session()->flash('blocked', 'true');
        return redirect('');
    }
});

I thought that it should call the function inside [privatecar::class, 'list'] and return the results but it just returns Array ( [0] => AppHttpControllersprivatecar [1] => list ) . How should I do to call the function?

PS: I am using laravel8

Advertisement

Answer

$param2 in Route::get($param1, $param2) is callable, but param2 in view($param1, $param2) only accept key/value array. Call them by yourself like this:

Route::get('/admin', function(){
    if (session()->has('username')) {
        $privatecars = (new privatecar())->list();
        $businesscars = (new businesscar())->list();
        return view('admin', ["privatecars" => $privatecars, "businesscars" => $businesscars]);
    } else {
        session()->flash('blocked', 'true');
        return redirect('');
    }
});
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement