I need to check if the data is exist and get data after that
Exam:
Loc::fisrt()->user()->exists()->get(); // This is just an example, it's not true
->get after exists this i need
I hope my question is clear.
CodePudding user response:
Just get the data and check it afterwards.
$user = Luc::first()->user;
If user() is a belongsTo, hasOne or morphOne relationship:
if ($user !== null) { /* user exists */ }
If user() is a belongsToMany, hasMany, hasManyThrough, morphMany, morphToMany or morphedToMany relationship:
$if ($user->isNotEmpty()) { /* user exists */ }
CodePudding user response:
There is a perfect solution for what you need in Eloquent already. The has method.
$results = Loc::has('user')->get();
The above results will only contain Loc records that have a user relation attached to them.
