Php 组合多个查询(Laravel 5)

Php 组合多个查询(Laravel 5),php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,我尝试为用户制作一个搜索引擎。搜索将包含多个字段,以便用户可以选择他想要的任何内容并获得结果 routes.php: Route::get('search/{tag?}/{town?}/{education?}/{contract?}', 'DisplayJobs@getSearch'); DisplayJobs.php控制器 public function getSearch($tag = null, $town = null, $education = null, $contrac

我尝试为用户制作一个搜索引擎。搜索将包含多个字段,以便用户可以选择他想要的任何内容并获得结果

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');
}

几个小时后,我找到了一个解决办法。我会把它贴在下面,这样如果有人有同样的问题可以看到一个解决方案

首先,我删除了模型中的所有范围查询以及控制器完成的所有工作,如bellow:

    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');
    $towns = \App\Town::lists('name', 'id');
    $contract = \App\Contract::lists('name', 'id');
    $education = \App\Education::lists('name', 'id');

    //get inputs from users
    $getTagFromUser = Input::get('tag');
    $getTownFromUser = Input::get('town');
    $getContractFromUser = Input::get('contract');
    $getEducationFromUser = Input::get('education');

    $tagQuery = DB::table('jobs')
        ->join('job_tag', 'jobs.id', '=', 'job_tag.job_id')
        ->join('tags', 'job_tag.tag_id', '=', 'tags.id')
        ->where('tags.id', '=', $getTagFromUser);

    $townQuery = DB::table('jobs')
        ->join('job_town', 'jobs.id', '=', 'job_town.job_id')
        ->join('towns', 'job_town.town_id', '=', 'towns.id')
        ->where('towns.id', '=', $getTownFromUser);

    $contractQuery = DB::table('jobs')
        ->join('job_contract', 'jobs.id', '=', 'job_contract.job_id')
        ->join('contracts', 'job_contract.contract_id', '=', 'contracts.id')
        ->where('contracts.id', '=', $getContractFromUser);

    $educationQuery = DB::table('jobs')
        ->join('job_education', 'jobs.id', '=', 'job_education.job_id')
        ->join('education', 'job_education.education_id', '=', 'education.id')
        ->where('education.id', '=', $getEducationFromUser);


    $finalQuery = $tagQuery->union($townQuery)->union($contractQuery)->union($educationQuery)->get();


    return view('main.search_jobs', compact('tags', 'towns', 'contract', 'education', 'finalQuery'));

}

几个小时后,我找到了一个解决办法。我会把它贴在下面,这样如果有人有同样的问题可以看到一个解决方案

首先,我删除了模型中的所有范围查询以及控制器完成的所有工作,如bellow:

    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');
    $towns = \App\Town::lists('name', 'id');
    $contract = \App\Contract::lists('name', 'id');
    $education = \App\Education::lists('name', 'id');

    //get inputs from users
    $getTagFromUser = Input::get('tag');
    $getTownFromUser = Input::get('town');
    $getContractFromUser = Input::get('contract');
    $getEducationFromUser = Input::get('education');

    $tagQuery = DB::table('jobs')
        ->join('job_tag', 'jobs.id', '=', 'job_tag.job_id')
        ->join('tags', 'job_tag.tag_id', '=', 'tags.id')
        ->where('tags.id', '=', $getTagFromUser);

    $townQuery = DB::table('jobs')
        ->join('job_town', 'jobs.id', '=', 'job_town.job_id')
        ->join('towns', 'job_town.town_id', '=', 'towns.id')
        ->where('towns.id', '=', $getTownFromUser);

    $contractQuery = DB::table('jobs')
        ->join('job_contract', 'jobs.id', '=', 'job_contract.job_id')
        ->join('contracts', 'job_contract.contract_id', '=', 'contracts.id')
        ->where('contracts.id', '=', $getContractFromUser);

    $educationQuery = DB::table('jobs')
        ->join('job_education', 'jobs.id', '=', 'job_education.job_id')
        ->join('education', 'job_education.education_id', '=', 'education.id')
        ->where('education.id', '=', $getEducationFromUser);


    $finalQuery = $tagQuery->union($townQuery)->union($contractQuery)->union($educationQuery)->get();


    return view('main.search_jobs', compact('tags', 'towns', 'contract', 'education', 'finalQuery'));

}

你的问题不清楚。您想要所有查询的组合结果数据,还是想要一种同时查询所有数据的方法?另外,没有一个
\App\Something::列出('name','id')工作,这让我想知道,你有没有运行代码?谢谢你的回复。所有的代码\App\Something::list('name','id');效果很好,我试过了。我只想合并所有查询的结果数据,或者一次查询所有数据的方法。它也适用于每个查询。您的问题不清楚。您想要所有查询的组合结果数据,还是想要一种同时查询所有数据的方法?另外,没有一个
\App\Something::列出('name','id')工作,这让我想知道,你有没有运行代码?谢谢你的回复。所有的代码\App\Something::list('name','id');效果很好,我试过了。我只想合并所有查询的结果数据或一次查询所有数据的方法。它也适用于每个查询。您的答案对我当前正在运行的项目非常有用,而我在几天前遇到了与您描述的问题相同的问题。您的答案对我当前正在运行的项目非常有用,而我以前也遇到过就在几天前,和你描述的问题一样。