我有三种型号:
和他们的关系是:
如何使用查询生成器来查询关系,直到获得下面的数据?
user_name: name,
image: [
{
...
},
{
...
}
],
post: [
{
...
},
{
...
}
]```
$user = User::find(1);
$images = $user->images // images is function name as u added in relationship
$posts = $user->posts // posts is function name as u added in relationship
$response = [
'user_name' => $user->name,
'images' => $images,
'posts' => $posts,
];
return response($response);
您也可以使用
$user = User::with(['images','posts'])->find(1);