Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rodney.hess's avatar

can you explain the with() method?

When doing something like:

return redirect('/')->with('success', 'item created');

What is the with() method? In this case, it isn't an Eloquent command (or is it?). I understand command chaining, but am not sure what the with() method is. I am not finding this in the Laravel docs.

TIA

0 likes
11 replies
Sergiu17's avatar

Read more about Eager Loading, well, with method is to Eager Load your relationships

rodney.hess's avatar

I have read that section...I am just not seeing how an Eloquent command that facilitates eager loading has to do with a redirect. I am missing something...

bobbybouwmann's avatar
Level 88

It depends on how you use the with method and on what class. For example when you call the method redirect() the class \Illuminate\Http\RedirectResponse is used. This method has a with function that will add a key value item to the session for that redirect.

Documentation: https://laravel.com/api/5.6/Illuminate/Http/RedirectResponse.html#method_with

However, when you use the with method on the view() method it will add a key and value item to the view. This way you can use that variable in your blade files

Documentation: https://laravel.com/api/5.6/Illuminate/View/View.html#method_with

So the with method is used on multiple places, but it depends on the class you call it on!

1 like
Cronix's avatar

@rodney.hess There are a LOT more functions covered in the api docs than the regular docs, fyi.

1 like
rodney.hess's avatar

@Cronix I am starting to see that...Thanks for the assist.

This has been the most responsive forum I have ever participated on.

1 like
freddy_m's avatar

you can use it like this in your blade @if (session('success')) {{ session('success') }}

Please or to participate in this conversation.