For each item in the collection to bring a Response?

public function view()
{
 $posts = DB::table('posts')
->select('*')
->get('*');

 foreach ($posts as $post) {
 echo $post->id.'<br>';
 return Response::json([
'posts'=>
[
'title'=>$post->title,
'datatime'=>$post->datetime,
'anons'=>$post->anons,
'text'=>$post->text,
 'tags'=>explode(', ', $post->tags),
'image'=>$post->images
]
]);
}
}


For each item in the collection $posts display your response in json format?
March 19th 20 at 08:50
2 answers
March 19th 20 at 08:52
No way. The server sends only 1 reply but not 10. Accordingly, the Response only 1. Return the entire collection.
March 19th 20 at 08:54
public function view()
{
 $posts = DB::table('posts')
->select('*')
->get('*');

 return Response::json([
 'posts' => array_map(function($post) {
 return [
'title'=>$post->title,
'datatime'=>$post->datetime,
'anons'=>$post->anons,
'text'=>$post->text,
 'tags'=>explode(', ', $post->tags),
'image'=>$post->images
];
 }, $posts)
]);
}

Find more questions by tags Laravel