I have an instance $person of Person and want to check if there is a relation created to a foreign entity (App).
people: id
people_apps: person_id, app_id
apps: id
The relationships are properly mapped in the Eloquent models. What is the preferred way to check this?
The only way I can think of is something like
$foundApp = $person->apps->filter(function($a) use($searchAppId)
{
return $a->id == $searchAppId;
});
if ($foundApp) {}
but there's probably a better way.
You can add a custom getter that checks in your model
class Person extends Eloquent {
public function getHasAppWithId($id){
return ($this->apps()->where('id', $id)->count() > 0);
}
}
In your code
$person->hasAppWithId(25);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With