Answer the question
In order to leave comments, you need to log in
How to display all fields in response?
There is a task
I got all the fields from the database
I'm trying to display, but it doesn't work, it returns only the first field
Here is the code itself
public function getPhotos() {
$photos = DB::table('photo')
->join('share', 'photo.id', '=', 'share.photo_id')->get();
$jsonPhotos = json_decode($photos , true);
dd($jsonPhotos);
for ($i = 0; $i < count($jsonPhotos); $i++) {
return response()->json(['id' => $jsonPhotos[$i]['id'], 'title' => $jsonPhotos[$i]['title'], 'url' => 'http://photoservice.ru/' . $jsonPhotos[$i]['path'], 'owner_id' => $jsonPhotos[$i]['owner_id'], 'users' => $jsonPhotos[$i]['user_id']], 200);
}
}
Answer the question
In order to leave comments, you need to log in
public function getPhotos() {
$photos = \DB::table('photo')
->join('share', 'photo.id', '=', 'share.photo_id')
->get()
->groupBy('photo_id')
->map(function ($photo) {
return [
'id' => $photo[0]->photo_id,
'title' => $photo[0]->title,
'url' => 'http://photoservice.ru/' . $photo[0]->path,
'owner_id' => $photo[0]->owner_id,
'users' => $photo->pluck('user_id')
];
})->values();
return response()->json($photos, 200);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question