Answers for "laravel check if relation is empty"

PHP
2

laravel check if laravel query is empty

if ($result->first()) { } 
if (!$result->isEmpty()) { }
if ($result->count()) { }
if (count($result)) { }
Posted by: Guest on May-18-2020
0

laravel check if relation is null

// filter models that do not have any related items:
1. Model::has('relation')->get()

// Once you have a model, if you already have loaded the collection, 
//  you can check the count of the collection:
2. $model->relation->count();

// If you want to check without loading the relation, 
// you can run a query on the relation:
3. $model->relation()->exists();

4. count($model->relation)
  
5. is_null($model->relation)
Posted by: Guest on June-29-2021

Code answers related to "laravel check if relation is empty"

Browse Popular Code Answers by Language