我有2张桌子
Entry
- id
- title
- submitter_name
- client_name
- agency_id
Agency
- id
- name
条目模型
public function agency(){
return $this->belongsTo('App\Agency');
}
和代理商模型
public function entries(){
return $this->hasMany('App\Entry', 'entry');
}
我需要根据标题,submitter_name,client_name,agency_name从数据库中找到所有条目
前三个很简单
public function searchByData(Request $request) {
$Entrys = Entry::where('title', 'LIKE', '%'.$request->title.'%')
->where('submitter_name', 'LIKE', '%'.$request->submitter_name.'%')
->where('client_name', 'LIKE', '%'.$request->client_name.'%')
->get();
return view('entries.index')->with($Entrys);
但是我在基于代理机构名称查找条目方面惨遭失败。
答案 0 :(得分:0)
尝试一下:
public function searchByData(Request $request) {
$Entrys = Entry::where('title', 'LIKE', '%'.$request->title.'%')
->where('submitter_name', 'LIKE', '%'.$request->submitter_name.'%')
->where('client_name', 'LIKE', '%'.$request->client_name.'%')
->whereHas('agency, function($query) use($request){
$query->where('name', 'LIKE', '%' . $request->agency_name . '%');
})
->get();
return view('entries.index')->with($Entrys);