Report between several tables with Eloquent Laravel

0

I have a many-to-many relationship between Users and Tasks

  

Users (id, name ..)

     

task_user (user_id, task_id)

     

Tasks (id, task, state) (STATE = finalized, process, delayed)

I need to bring the number of total tasks per user and how many are completed, in process, delayed, which is not coming out.

For example

Juan Lopez
 Total tasks 35
 In process: 7  Delayed: 9

    
asked by Juan Pablo B 20.12.2017 в 13:23
source

1 answer

1

Something like that could guide you a bit:

You bring the eager users loadeando the tasks in the controller and you pass it to the view:

$usuarios = Usuario::with("tareas")->get();
return view('unavista', compact("usuarios"));

Then in the view:

@foreach($usuarios as $usuario){
    nombre:{{$usuario->nombre}}<br>
    Tareas totales: {{$usuario->tareas->count()}}<br> 
    En proceso: {{$usuario->tareas->where('estado', 'proceso')->count()}}<br> 
    Demoradas: {{$usuario->tareas->where('estado', 'demorado')->count()}}<br>
}

UPDATE 2 ° Query:

This should work (in the controller):

$usuarios= Usuario::with("tareas")
->withCount('tareas')
->orderBy('tareas_count', 'desc')
->get();
    
answered by 20.12.2017 / 13:39
source