当我使用此代码时:
Route::get('/user/{id}/posts', function ($id){
$post = User::find($id)->postst;
return $post;
});
输出为:
[
{
"id":1,
"user_id":1,
"title":"Eloquent Basic Insert",
"content":"By eloquent we can easily insert a data and it is awesome",
"created_at":"2018-05-15 14:45:34",
"updated_at":"2018-05-15 14:45:34",
"deleted_at":null
},
{
"id":2,
"user_id":1,
"title":"Add with create",
"content":"This might be fail",
"created_at":"2018-05-15 14:47:59",
"updated_at":"2018-05-15 14:47:59",
"deleted_at":null
}
]
但当我使用foreach循环时,它只显示一个数组
Route::get('/user/{id}/posts', function ($id){
$post = User::find($id)->postst;
foreach ($post as $post){
return $post->title. '<br>';
}
});
此代码的输出为:
雄辩的基本插入
如何在浏览器上显示阵列的所有标题?我的代码有什么问题?