合并多个查询(Laravel 5) [英] Combine multiple queries (Laravel 5)

查看:241
本文介绍了合并多个查询(Laravel 5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为用户创建搜索引擎.搜索将具有多个字段,以便用户可以选择他想要的任何内容并获得结果.

I try to make a search engine for users. The search will be with multiple fields so as the user can be selecting whatever he want and get the result.

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

}

如果我尝试使用每个查询,它都能很好地工作,但是我想合并所有查询的结果数据,或者想一次查询所有数据的方法.

If I try with each single query it works perfectly but I want to combined result data from all the queries or a way to query all the data at once.

在每个模型中,我都有这样的查询范围:(Tag.php)模型:

In each model I have my query scope like this (Tag.php) Model:

    public function jobs()
{
    return $this->belongsToMany('App\Job');
}

public function scopeTagOfUser($query, $tag)
{
    return $query->where('id', '=', $tag)->with('jobs');
}

推荐答案

很多小时后,我找到了解决方案.我将其张贴在下面,因此,如果有人遇到相同的问题,可以看到一种解决方案.

After a lot of hours I found a solution. I will post it below so if anyone has the same problem can see one solution.

首先,我删除了模型中的所有作用域查询,并且像波纹管一样完成了控制器的所有工作:

First I have delete all of the scope queries in the models and all of the work completed to the controller like 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'));

}

这篇关于合并多个查询(Laravel 5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆