1
我尝试为用户创建搜索引擎。搜索将包含多个字段,以便用户可以选择他想要的任何内容并获得结果。合并多个查询(Laravel 5)
routes.php文件:
Route::get('search/{tag?}/{town?}/{education?}/{contract?}', '[email protected]');
DisplayJobs.php控制器
public function getSearch($tag = null, $town = null, $education = null, $contract = null)
{
//get already database values to send them to the form
$tags = \App\Tag::lists('name', 'id');
$contract = \App\Contract::lists('name', 'id');
$towns = \App\Town::lists('name', 'id');
$education = \App\Education::lists('name', 'id');
$tagQueryBuilder = Tag::query();
$townQueryBuilder = Town::query();
$educationQueryBuilder = Education::query();
$contractQueryBuilder = Contract::query();
if(Input::has('tag'))
{
$tagQueryBuilder->TagOfUser(Input::get('tag'));
}
if(Input::has('town'))
{
$townQueryBuilder->TownOfUser(Input::get('town'));
}
if(Input::has('education'))
{
$educationQueryBuilder->EducationOfUser(Input::get('education'));
}
if(Input::has('contact'))
{
$contractQueryBuilder->ContactOfUser(Input::get('contact'));
}
return view('main.search_jobs', compact('tags', 'towns', 'contract', 'education'));
}
如果我尝试用它完美的每个单独的查询,但我想综合作用的结果数据从所有查询或一次查询所有数据的方式。
在每一个模型,我有我的查询范围是这样(Tag.php)型号:
public function jobs()
{
return $this->belongsToMany('App\Job');
}
public function scopeTagOfUser($query, $tag)
{
return $query->where('id', '=', $tag)->with('jobs');
}
你的问题不明确。你想要所有查询的组合结果数据,还是想要一种方法一次查询所有数据?此外,'\ App \ Something :: lists('name','id');'工作,这让我想知道,你甚至没有运行代码? – JSelser
谢谢你的回复。所有代码\ App \ Something :: lists('name','id');完美的作品,我尝试过。我只想结合所有查询的结果数据或一次查询所有数据的方法。 –
此外,它适用于每个单一的查询。 –