laravel check if relation is null

Solutions on MaxInterview for laravel check if relation is null by the best coders in the world

showing results for - "laravel check if relation is null"
Emilia
15 May 2016
1// filter models that do not have any related items:
21. Model::has('relation')->get()
3
4// Once you have a model, if you already have loaded the collection, 
5//  you can check the count of the collection:
62. $model->relation->count();
7
8// If you want to check without loading the relation, 
9// you can run a query on the relation:
103. $model->relation()->exists();
11
124. count($model->relation)
13  
145. is_null($model->relation)