Level 55
$userOnLeave = DB::table('users')
->select('users.*', 'leaves.to')
->join('leaves', 'users.id', 'leaves.user_id')
->get()
->groupBy('to');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
users
id, name, email
1, john, [email protected]
2, martin, [email protected]
3, bob, [email protected]
leaves
id, user_id, from, to
1, 1, , 2021-09-01, 2021-09-05
2, 2, , 2021-09-05, 2021-09-05
3, 3, , 2021-09-02, 2021-09-05
//From this table i want data like key as date and value as user_id
'2021-09-01' => [
0 => 1
]
'2021-09-02' => [
0 => 1,
1 => 3
]
'2021-09-03' => [
0 => 1,
1 => 3
]
'2021-09-04' => [
0 => 1,
1 => 3
]
'2021-09-05' => [
0 => 1,
1 => 2,
2 => 3,
]
$userOnLeave = DB::table('users')
->join('leaves', function ($innerJoin) use ($query) {
$innerJoin->on('users.id', '=', 'leaves.user_id')
->whereRaw(?)
})->get();
$userOnLeave = DB::table('users')
->select('users.*', 'leaves.to')
->join('leaves', 'users.id', 'leaves.user_id')
->get()
->groupBy('to');
Please or to participate in this conversation.