如何在Laravel上附加额外数据
我有一个函数可以加载帖子如何在Laravel上附加额外数据,laravel,eager-loading,Laravel,Eager Loading,我有一个函数可以加载帖子 public function getUserPosts() { return User::with('posts')->where("organization_id", $this->getOrganization()->id)->get()->toArray(); } 这将返回良好的用户帖子,我的问题是我可以添加一些额外的数据加载类似的东西吗 public function getUserPosts() {
public function getUserPosts()
{
return User::with('posts')->where("organization_id", $this->getOrganization()->id)->get()->toArray();
}
这将返回良好的用户帖子,我的问题是我可以添加一些额外的数据加载类似的东西吗
public function getUserPosts()
{
return User::with('posts', function(){
//say I fetch an array of posts here,
// I want to attach this array of posts with the eagerloaded posts
})->where("organization_id", $this->getOrganization()->id)->get()->toArray();
}
返回用户::带有['posts',函数$query{
//这里有对关系的查询,可以使用它进行普通的查询
//像这样:
$query->with'comments'->选择'id','title'->where'created_at','>',Carbon::now;
}]->其中Organization\u id,$this->getOrganization->id->get->toArray;