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

p0t4t0's avatar

What is with() for?

What does it do and what is its use cases? I would really appreciate an example or two

0 likes
9 replies
tykus's avatar

with in the context of returning a view: passes data to the view

return view('view.name')->with('var', $var);

with in the context of Eloquent is for eager-loading relations - this goes to reducing the number of queries being made on the database since we are not fetching all of the Post instances and the as we iterate of them, make a query to fetch the Comments attached to each Post:

Post::with('comments')->get();

with in the context of a response flashes a key/value pair the session

return back()->with('key', $value);
1 like
p0t4t0's avatar

@tykus so if with is used with a view then it is the same as using compact()? About with used with Eloquent... what is the equivalent of your example when used without with? (the one which makes use of multiple queries)

jlrdw's avatar

This is in the documentation so I don't follow asking it here, sorry. Taylor actually gives excellent examples.

Snapey's avatar

If you said

$posts = Post::all();

and then in a loop you did {{ $post->comment }} then a query would run every pass of the loop to load a single comment.

if you said

$posts = Post::with('comments')->get();

then not only are the posts loaded from the database, but also all the comments that are related to the posts collection.

Then when iterating over the results, all the comments are to hand and can be used without re-querying the database

1 like
p0t4t0's avatar

@jlrdw yes I am aware most of the info has already been documented in the docs but not all of us are native English speakers. I am posting here to get as simple an explanation I can get.

@Snapey thank you. I think I understand now. There is just one last thing I need clarification with. When using with with Eloquent, is it safe to assume that it can only be used on models that have optional relations to other tables? like posts could have comments so we use Post::with('comments') but a post must have a user so we can't do Post::with('user')... or if we go further down, with can only be used on models with the hasOne or hasMany function in them? and not the ones with the belongsTo?

jlrdw's avatar

@p0t4t0 I understand, you probably need to view a video on the n+1 problem.

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

with can be used on any model that has a relation, even if that is via a pivot table.

with('nameOfRelationship') where nameOfRelationship is found on the model

Also, you can also load distant relations. So in the classic posts and comments models, a comment may be written by an author

You can dot separate the models;

$posts = Post::with('comments.author')->get();

and now comments for posts is loaded, as well as author for comments. In this case author must be a method on the Comment model

and author does not need to match the model, for instance;

Comment.php

public function author()
{
        return $this->belongsTo(User::class, 'author_id','id');
}

1 like
p0t4t0's avatar

@Snapey I tried using with() in my test app and I just need one last recap

$posts = Post::with('comments')->get();

$posts = Post::all();

The first line of code is better because we do not have to query the database for a second time when say I call $post->comments when using the second line of code in my view. Did I get that right?

Please or to participate in this conversation.