Laravel - 模型不会传递给 first() 方法的闭包


Laravel - A model is not passed to first() method's closure

我有一个collection,我正在用闭包调用first()方法,期望一个参数说$model

执行时,如果我尝试访问 $model 的属性;它说:

访问非对象的属性

我尝试转储$model,发现它有一个整数1而不是Object

$Collection->first( function($model) {
    if(!$model) return false;
    return $model->type == 'Test';
});

我只是让它工作。first()方法的参数闭包提供 2 个变量。第一个是关键,第二个是模型。因此,您将调用如下所示first()方法,如果它满足您定义的条件,只需返回true

$result = $collection->first( function($i, $model) {
    // define criteria and return true if it satisfies. The model will now be returned in resulting Collection.
});