如何从laravel 5中的相关表中获取数据?
How to get data from related tables in laravel 5?
我的页面上有一个搜索表单,可以搜索多个表格,我该如何执行以下操作并正确搜索?我希望能够搜索机构名称、客户名称和类型名称以及用户名和密码:
我的视图如下所示
@foreach ($accounts as $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
用户名和密码搜索正确,但连接没有带来任何结果
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'));
}
更新::
这有效:
->Join('types', 'accounts.type_id', '=', 'types.id')
->Join('agencies', 'accounts.agency_id', '=', 'agencies.id')
->Join('clients', 'accounts.client_id', '=', 'clients.id')
您的加入似乎有误
->Join('types', 'accounts.id', '=', 'accounts.type_id')
->Join('agencies', 'accounts.id', '=', 'accounts.agency_id')
->Join('clients', 'accounts.id', '=', 'accounts.client_id')
了解如何在“=”的两侧使用帐户 table 您需要一侧有 types/agencies/clients 才能加入。
所以它会是这样的
->Join('types', 'accounts.id', '=', 'types.type_id')
->Join('agencies', 'accounts.id', '=', 'agencies.agency_id')
->Join('clients', 'accounts.id', '=', 'clients.client_id')
说这需要您的数据库结构来查明为什么您没有从连接查询搜索中获得结果。
我的页面上有一个搜索表单,可以搜索多个表格,我该如何执行以下操作并正确搜索?我希望能够搜索机构名称、客户名称和类型名称以及用户名和密码:
我的视图如下所示
@foreach ($accounts as $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
用户名和密码搜索正确,但连接没有带来任何结果
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'));
}
更新:: 这有效:
->Join('types', 'accounts.type_id', '=', 'types.id')
->Join('agencies', 'accounts.agency_id', '=', 'agencies.id')
->Join('clients', 'accounts.client_id', '=', 'clients.id')
您的加入似乎有误
->Join('types', 'accounts.id', '=', 'accounts.type_id')
->Join('agencies', 'accounts.id', '=', 'accounts.agency_id')
->Join('clients', 'accounts.id', '=', 'accounts.client_id')
了解如何在“=”的两侧使用帐户 table 您需要一侧有 types/agencies/clients 才能加入。
所以它会是这样的
->Join('types', 'accounts.id', '=', 'types.type_id')
->Join('agencies', 'accounts.id', '=', 'agencies.agency_id')
->Join('clients', 'accounts.id', '=', 'clients.client_id')