Php laravel 5.5雄辩,搜索关系集合
在我的Prospect表中,我有几个列的id存储为int,表示其他表的项(Php laravel 5.5雄辩,搜索关系集合,php,search,eloquent,laravel-5.5,Php,Search,Eloquent,Laravel 5.5,在我的Prospect表中,我有几个列的id存储为int,表示其他表的项(客户id、顾问id和房屋id),其他列是直接数据,例如街道、房屋编号,等等。我用雄辩的关系连接id,但无法确定是否有我的索引列表来搜索这些关系集合。示例client->first\u name,client->email或house->name 我如何让我的搜索进行查看?例如,如果客户端->名称为John,则在搜索“John”时显示? 我有: Prospect.php模型: protected $fillable
客户id、顾问id和房屋id
),其他列是直接数据,例如街道、房屋编号
,等等。我用雄辩的关系连接id,但无法确定是否有我的索引列表来搜索这些关系集合。示例client->first\u name,client->email或house->name
我如何让我的搜索进行查看?例如,如果客户端->名称为John,则在搜索“John”时显示?
我有:
Prospect.php模型:
protected $fillable = ['consultant_id', 'client_id', 'house_id', 'house_spec', 'elevation', 'lot_no', 'house_no', 'street', 'suburb', 'shire', 'estate_name', 'details', 'stage'];
public function client()
{
return $this->belongsTo('App\Client');
}
public function consultant()
{
// return $this->belongsTo('App\User', 'consultant', 'id');
return $this->belongsTo(User::class);
}
public function house()
{
return $this->belongsTo('App\Houses');
}
还有我的ProspectController.php
public function index(Request $request)
{
$keyword = $request->get('search');
$perPage = 25;
if (!empty($keyword)) {
$prospects = Prospect::where('client_id', 'LIKE', "%$keyword%")
->orWhere('house_id', 'LIKE', "%$keyword%")
->orWhere('house_spec', 'LIKE', "%$keyword%")
->orWhere('elevation', 'LIKE', "%$keyword%")
->orWhere('lot_no', 'LIKE', "%$keyword%")
->orWhere('house_no', 'LIKE', "%$keyword%")
->orWhere('street', 'LIKE', "%$keyword%")
->orWhere('suburb', 'LIKE', "%$keyword%")
->orWhere('shire', 'LIKE', "%$keyword%")
->orWhere('estate_name', 'LIKE', "%$keyword%")
->orWhere('details', 'LIKE', "%$keyword%")
->orWhere('details', 'LIKE', "%$keyword%")
->paginate($perPage);
} else {
$prospects = Prospect::with('client')
->with('house')->paginate($perPage);
}
return view('prospects.index', compact('prospects'));
}
使用或wherehas()
完美而优雅@sohel0415,我已经找了好几天了。这就是我正在做的,但客户表在另一个数据库中。它不是maindb.clients,而是anotherdb.clients,(my.env有DB_DATABASE_2=anotherdb,这叫做mysql2,所以我得到了这个错误:“SQLSTATE[42S02]:找不到基表或视图:1146表“maindb.clients”不存在我必须让它查看另一个db.clients..有什么想法吗?感谢您可以在客户端模型中更改连接逻辑,在模型类中添加此行
protected$connection='mysql2';
此链接可能会帮助您完善,这看起来就像我提到的地方当我建立第二个数据库时,我真的需要保存这个链接。我想我需要的是运行时的连接,因为其余的我都有了,再次感谢@sohel0405,你帮了我一天
$prospects = Prospect::where('client_id', 'LIKE', "%$keyword%")
->orWhere('house_id', 'LIKE', "%$keyword%")
->orWhere('house_spec', 'LIKE', "%$keyword%")
->orWhere('elevation', 'LIKE', "%$keyword%")
->orWhere('lot_no', 'LIKE', "%$keyword%")
->orWhere('house_no', 'LIKE', "%$keyword%")
->orWhere('street', 'LIKE', "%$keyword%")
->orWhere('suburb', 'LIKE', "%$keyword%")
->orWhere('shire', 'LIKE', "%$keyword%")
->orWhere('estate_name', 'LIKE', "%$keyword%")
->orWhere('details', 'LIKE', "%$keyword%")
->orWhere('details', 'LIKE', "%$keyword%")
->orWhereHas('client',function($q) use ($keyword){
$q->where('first_name', 'LIKE', "%$keyword%")->orWhere('email', 'LIKE', "%$keyword%");
})
->paginate($perPage);