Skip to content
Advertisement

Laravel 9 with argument 2 must be of type ?callable, string given

I’m following a tutorial. In the controller I’m using with to send a success message.

    public function store(Request $request)
{
    $request->validate([
       'recipe' => 'required',
        'rating' => 'required',
    ]);

    Recipe::create($request->all());

    return redirect()->route('recipes.index')
        -with('success', 'Recipe created successfully');
}

I’m getting an error message once the form is submitted and I’m redirected to the index page the code there looks like this.

@if ($message = IlluminateSupportFacadesSession::get('success'))
    <div class="alert alert-success">
        <p>{{ $message }}</p>
    </div>
@endif

This is the error message: with(): Argument #2 ($callback) must be of type ?callable, string given, called in C:laragonwwwrecipe_projectappHttpControllersRecipeController.php on line 49

Advertisement

Answer

You missed arrow:

return redirect()->route('recipes.index')
        -with('success', 'Recipe created successfully');

Should be:

return redirect()->route('recipes.index')
        ->with('success', 'Recipe created successfully');
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement