我需要别名的时候我做了Laravel预先加载:如何别名Laravel预先加载
$posts = Post::with(array('images as main_image' => function($query) // do not run
{
$query->where('number', '=', '0');
}))
->where('id', '=', $id)
->get();
return Response::json($posts);
我需要这个,因为我想这样的JSON响应:
[
{
"id": 126,
"slug": "abc",
"name": "abc",
"created_at": "2014-08-08 08:11:25",
"updated_at": "2014-08-28 11:45:07",
"**main_image**": [
{
"id": 223,
"post_id": 126
...
}
]
}
]
有可能的?
你可以用一个单一的与'后写查询::与(” main_image','gallery_image')' – Needpoule 2014-08-29 09:02:48
完美!谢谢 – kurtko 2014-08-30 12:19:07