how to filter data from a table

Asked

Viewed 29 times

0

I have a table estagiario related to a table areas, and the table areas in turn this related to the table departamento.

I can even list all the interns used the following function:

public function index(){
        $data = date("Y-m-d");
        $terminados = Estagiario::whereDate('fim_estagio', '<=', $data)->paginate(6);       
        return view('estagiarios.terminado',compact('terminados'));
    }

And I’m having trouble having interns who are associated with a certain department.

public function search(Request $request){
        $search=$request->get('Departamento');
        $data = date("Y-m-d");
        $terminados =Estagiario::whereDate('fim_estagio', '<=', $data)
                            ->where('areas.departamento.name','=', $search)
                            ->paginate(6);
    return view('estagiarios.terminado',compact('terminados'));
    }
  • 1

    Please present the formatted code.

1 answer

0

Hello.

I believe that the relationships between the tables in your query were not specified.

By the code you presented, you are filtering by a parameter that is in the third entity but lacked to specify the relationships between them in the query.

An example for such a query would be something like the code below:

public function search(Request $request){
    $search=$request->get('Departamento');
    $data = date("Y-m-d");
    $terminados =Estagiario::whereDate('fim_estagio', '<=', $data)
                        ->join('areas', 'areas.id', '=', 'estagiario.id')
                        ->join('departamento', 'departamento.id', '=', 'areas.id')
                        ->where('departamento.name','=', $search)
                        ->paginate(6);
return view('estagiarios.terminado',compact('terminados'));

}

Laravel documentation here 'JOIN'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.