[英]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')
查看使用帐户表在'='的两侧如何需要一侧具有类型/代理机构/客户才能加入工作。
所以这就像
->Join('types', 'accounts.id', '=', 'types.type_id')
->Join('agencies', 'accounts.id', '=', 'agencies.agency_id')
->Join('clients', 'accounts.id', '=', 'clients.client_id')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.