在我的模型中,我有一个集合,我试图在我的模型中返回它,而不是关系对象。
这样我就可以调用 $user->items
并获取该集合。
在模型中,函数如下所示:
class User extends Model {
public function channelsAttribute() {
$name = Company::where('id', $this->id)->first()
$items = Item::where('company_id', $name)->get();
return $items;
}
}
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
由于有两种方式,我找不到使用关系的方法;但是 $items
返回值的集合。
我应该做什么?
最佳答案
您没有正确调用该方法,而是
function channelsAttribute()
你需要做
function getChannelsAttribute()
关于php - 在 Eloquent 模型中返回集合而不是关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43300850/