【发布时间】:2015-10-06 19:17:59
【问题描述】:
我尝试为用户制作搜索引擎。搜索将包含多个字段,以便用户可以选择他想要的任何内容并获得结果。
routes.php:
Route::get('search/{tag?}/{town?}/{education?}/{contract?}', 'DisplayJobs@getSearch');
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');没有一个工作,这让我想知道,你甚至运行过代码吗? -
感谢您的回复。所有代码 \App\Something::lists('name', 'id');完美运行,我试过了。我只想合并所有查询的结果数据或一次查询所有数据的方法。
-
它也适用于每个查询。
标签: php laravel laravel-5 eloquent