Answer the question
In order to leave comments, you need to log in
How to correctly form a query to the db in laravel?
There are three tables - users, images, user groups. The essence of the request is to select all user data from 1 user group and attach all attached images to them:
<?php
Route::get('/get/database/girls', function () {
$users = DB::table('users')
->join('users_groups', function ($join_groups) {
$join_groups->on('users.id', '=', 'users_groups.user_id');
})
->join('system_files', function ($join_images) {
$join_images->on('users.id', '=', 'system_files.attachment_id');
})
->where('users_groups.user_group_id', '=', 1)
->get();
return json_encode($users);
});
Answer the question
In order to leave comments, you need to log in
$users = DB::select(
"select
t1.id,
t1.user_name,
GROUP_CONCAT( t3.field SEPARATOR ',') as images
from users t1
join users_groups t2 on t2.user_id = t1.id
join system_files t3 on t3.user_id = t1.id
where t2.user_group_id = 1
group by t1.id, t1.user_name");
return json_encode($users);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question