AdRock
3 months ago

How to check if relationship exists in Elqouent collection

Posted 3 months ago by AdRock

I am building an Eloquent collection based on what the user has selected on a form. If they check specific checkboxes on the form, I dynamically create a relation. This works perfectly.

$collection = MyModel::when(!is_null($request->input('make')), function($query) {
    $query->with('make');
 })

When I do dd($collection) I see the relations

How can I check for the existence of the relation? I have tried dd($collection->contains('make')); but it return false.

EDIT

I have tried this but it returns true even if it doesn't exist in the collection

$test = $collection->contains(function ($value, $key) {
    return $value['make'];
});

Please sign in or create an account to participate in this conversation.