我需要別名的時候我做了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