【发布时间】:2018-05-15 15:40:00
【问题描述】:
当我使用这段代码时:
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>';
}
});
这段代码的输出是:
Eloquent 基本插入
如何在浏览器上显示数组的所有标题?我的代码有什么问题?
【问题讨论】:
-
return被放置在你的 for 循环中。 -
谢谢...echo解决了这个问题