尝试将新属性添加到现有集合并访问它。
我需要的是这样的:
$text = Text::find(1); //Text model has properties- id,title,body,timestamps
$text->user = $user;
并通过 , 访问用户$text->user
。
探索文档和 SO,我发现put
, prepend
,setAttribute
方法来做到这一点。
$collection = collect();
$collection->put('a',1);
$collection->put('c',2);
echo $collection->c; //Error: Undefined property: Illuminate\Support\Collection::$c
再次,
$collection = collect();
$collection->prepend(1,'t');
echo $collection->t = 5; //Error: Undefined property: Illuminate\Support\Collection::$t
和
$collection = collect();
$collection->setAttribute('c',99); // Error: undefined method setAttribute
echo $collection->c;
有什么帮助吗?