I need help creating a query or some way to do a count. Context:
I have two tables:
user: where I have my users or workers
work: where I will assign a task to a user with statuses (complete, in process, error, etc).
My problem arises when I want to pass my employees in a select to my view: I can do that easily, but at the same time I need to know the jobs that each employee has in progress, example:
<select class="form-control mb-2" name="" required>
<option selected> Seleccione el trabajador </option>
<option value="">Jose - 2 trabajos asignados en curso</option>
<option value="">Luis - 5 trabajos asignados en curso</option>
</select>
So that in my view my workers are seen that way with the number of jobs assigned but from least to greatest.
Assuming the structure of user is
and the work
go
id_user
condition
$query = DB::table('user', 'u')->select('count(t.id_user) as numberofjobs', 'u.name as name')->join('job as t', ' u.id','=','t.id_user')->groupBy('name')->orderBy('numberofjobs','desc')->get();
This will return https://isstatic.aoverflow.com/71AO3.jpg
And if you want me to also bring you those who don't have jobs, just put a Left JOIN
it's very simple in case you need to add additional text you can use a foreach
with this code the following JSON votes for me