Answer the question
In order to leave comments, you need to log in
Is there any way to simplify this selection?
Are there no arrow functions in PHP?
$posts = Post::where('user_id', 1)
->orderBy('created_at', 'desc')
->get();
$res = $posts->map(function ($item, $key) {
$tags = $item->tags->map(function ($tag) {
return [
'id' => $tag->id,
'name' => $tag->name
];
});
return [
'id' => $item->id,
'user' => $item->user,
'tags' => $tags,
'title' => $item->title,
'type' => $item->type
];
});
return response()->json($res);
$tags = $item->tags->map(function ($tag) {
return [
'id' => $tag->id,
'name' => $tag->name
];
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question