I've found the concept and meaning behind these methods to be a little confusing, is it possible for somebody to explain to me what the difference between has
and with
is, in the context of an example (if possible)?

- 147,337
- 26
- 332
- 270
-
**See also** stackoverflow.com/[Laravel "scope" query inside join?](https://stackoverflow.com/questions/70678024/laravel-how-to-use-model-scope-method-inside-join-query) – Top-Master Apr 01 '22 at 07:40
2 Answers
With
with()
is for eager loading. That basically means, along the main model, Laravel will preload the relationship(s) you specify. This is especially helpful if you have a collection of models and you want to load a relation for all of them. Because with eager loading you run only one additional DB query instead of one for every model in the collection.
Example:
User > hasMany > Post
$users = User::with('posts')->get();
foreach($users as $user){
$users->posts; // posts is already loaded and no additional DB query is run
}
Has
has()
is to filter the selecting model based on a relationship. So it acts very similarly to a normal WHERE condition. If you just use has('relation')
that means you only want to get the models that have at least one related model in this relation.
Example:
User > hasMany > Post
$users = User::has('posts')->get();
// only users that have at least one post are contained in the collection
WhereHas
whereHas()
works basically the same as has()
but allows you to specify additional filters for the related model to check.
Example:
User > hasMany > Post
$users = User::whereHas('posts', function($q){
$q->where('created_at', '>=', '2015-01-01 00:00:00');
})->get();
// only users that have posts from 2015 on forward are returned

- 2,992
- 3
- 29
- 42

- 147,337
- 26
- 332
- 270
-
202+1, very helpful answer! Note also that while `with('relation')` will include the related table's data in the returned collection, `has('relation')` and `whereHas('relation')` will *not* include the related table's data. So you may need to call both `with('relation')` as well as `has()` or `whereHas()`. – Soulriser Feb 06 '16 at 16:08
-
2Greet Answer, How to access parent model from relationship model for instance here how to search post model based on the attributes of user model – Hussain Rahimi May 12 '17 at 05:53
-
@BhojendraNepal Unfortunately there doesn't seem to be a lot about it in the docs... [This is all I found](https://laravel.com/docs/5.4/eloquent-relationships#eager-loading) (it's a few paragraphs down) – lukasgeiter Jul 25 '17 at 21:00
-
@hussainfrotan the same way, use `whereHas` on user relation when querying post. – Michael Tsang Nov 12 '18 at 05:35
-
Curious, in Laravel documentation: https://laravel.com/docs/5.8/eloquent-relationships , when using `whereHas` it uses `use Illuminate\Database\Eloquent\Builder;` which then is with `function(Builder $query)`. Most examples that I seen, dot use the `Builder`, just pass in the $query, which is the right way? – Guntar Feb 22 '20 at 17:21
-
-
@Guntar if you mean the example is like this `function($query)...` it is the same thing without the type hints. – darighteous1 Jun 24 '22 at 04:46
-
1@lukasgeiter worthing updating answer Laravel 9.16 includes a `withWhereHas` – John Magnolia Nov 13 '22 at 18:37
The documentation has already explained the usage, so I will use SQL to explain the methods.
Example:
Assuming there is an Order (orders)
has many OrderItem (order_items)
and you already built the relationship between them:
// App\Models\Order:
public function orderItems() {
return $this->hasMany('App\Models\OrderItem', 'order_id', 'id');
}
These three methods are all based on a relationship.
with
Result: with()
return the model object and its related results.
Advantage: It is eager-loading which can prevent the N+1 problem.
When you are using the following Eloquent Builder:
Order::with('orderItems')->get();
Laravel change this code to only two SQL:
// get all orders:
SELECT * FROM orders;
// get the order_items based on the orders' id above
SELECT * FROM order_items WHERE order_items.order_id IN (1,2,3,4...);
And then Laravel merges the results of the second SQL query with the results of the first SQL by foreign key, finally returning the collection results.
So if you selected columns without the foreign_key in a closure, the relationship result will be empty:
Order::with(['orderItems' => function($query) {
// $query->sum('quantity');
$query->select('quantity'); // without `order_id`
}
])->get();
#=> result:
[{ id: 1,
code: '00001',
orderItems: [], // <== is empty
},{
id: 2,
code: '00002',
orderItems: [], // <== is empty
}...
}]
has
Has
will return the model's object when its relationship is not empty.
Order::has('orderItems')->get();
Laravel changes this code to one SQL query:
select * from `orders` where exists (
select * from `order_items` where `orders`.`id` = `order_items`.`order_id`
)
whereHas
The methods whereHas
and orWhereHas
put where
conditions on your has
queries. These methods allow you to add customized constraints to a relationship constraint.
Order::whereHas('orderItems', function($query) {
$query->where('status', 1);
})->get();
Laravel changes this code to one SQL query:
select * from `orders` where exists (
select *
from `order_items`
where `orders`.`id` = `order_items`.`order_id` and `status` = 1
)