mysql - How to get data from related tables in laravel 5? -


i have search form on page searches through several tables, how do below , search correctly? want able search agency name, client name , type name along username , password:

my view looks below

@foreach ($accounts $account)                 <tr>                     <td> {{$account->client->name}} </td>                     <td> {{$account->agency->name}} </td>                     <td> {{$account->type->name}} </td>                     <td> {{$account->username}} </td>                     <td> {{$account->password}} </td>                     <td><a href="{{route('accounts.edit',$account->id)}}"><span class="fa fa-edit"></span></a></td>                 </tr>                 @endforeach 

username , password searches correctly joins dont bring results

    public function index(){      $search = \request::get('search');      $accounts =         account::where('clients.name','like','%'.$search.'%')         ->orwhere('agencies.name','like','%'.$search.'%')         ->orwhere('types.name','like','%'.$search.'%')         ->orwhere('username','like','%'.$search.'%')         ->orwhere('password','like','%'.$search.'%')         ->join('types', 'accounts.id', '=', 'accounts.type_id')         ->join('agencies', 'accounts.id', '=', 'accounts.agency_id')         ->join('clients', 'accounts.id', '=', 'accounts.client_id')         ->paginate(20);        return view('accounts',compact('accounts'));     } 

update:: worked:

       ->join('types', 'accounts.type_id', '=', 'types.id')        ->join('agencies', 'accounts.agency_id', '=', 'agencies.id')        ->join('clients', 'accounts.client_id', '=', 'clients.id') 

your join seems wrong

->join('types', 'accounts.id', '=', 'accounts.type_id') ->join('agencies', 'accounts.id', '=', 'accounts.agency_id') ->join('clients', 'accounts.id', '=', 'accounts.client_id') 

see how on both side of '=' using accounts table need have 1 side have types/agencies/clients join work.

so like

->join('types', 'accounts.id', '=', 'types.type_id') ->join('agencies', 'accounts.id', '=', 'agencies.agency_id') ->join('clients', 'accounts.id', '=', 'clients.client_id') 

saying need db structure pin point why not getting result join query search.