php - Laravel - A model is not passed to first() method's closure
I have a{-code-1}
and I am callingfirst()
method on it with a closure expecting a parameter say$model
.
Upon execution, if I try to access a property of$model
; it says:
Accessing property of non-object
I tried dumping$model
and found that it has a integer1
instead ofObject
.
$Collection->first( function($model) {
if(!$model) return false;
return $model->type == 'Test';
});
Answer
Solution:
I just got it working. The
first()
method's argument closure is provided with 2 variables. First one is the key and second one is the model. So, you will callfirst()
method like below and if it satisfies the criteria you defined, just returntrue
.