【问题标题】:Display information of 1 table from 2 columns in the the search results. (PHP - Laravel)在搜索结果中显示 2 列中 1 个表的信息。 (PHP - Laravel)
【发布时间】:2019-07-10 06:21:58
【问题描述】:

我有一个网页可以让你浏览不同的工作机会,这些工作机会保存在一个名为 “jobs”的 MySQL 表中

“jobs” 有不同的列,其中之一是 “basic” 用于正常工作,“ op2” 适用于优质工作。

在执行搜索时,我试图在我的结果中显示这两个类别,但是我只能显示一个,而不是两个。

我能够使用以下方法显示这两个作业:


 $premium_jobs = Posts::where('title', 'like', '%' . $request->q . '%')->where('type', 'op2')->where('status', 'active')->orderBy('id', 'desc')->take(3)->get();

但是,当使用过滤器搜索特定字词时,它适用于基本职位,但会显示所有高级职位而不应用任何过滤器。


一些无效的查询:

  • 搜索基本工作,但始终显示所有高级工作,无论搜索是什么:

    $premium_jobs = Posts::where('title', 'like', '%' . $request->q . '%')->where('type', 'op2')->where('status', 'active')->orderBy('id', 'desc')->take(3)->get();
    

    Example

  • 不显示高级职位,仅显示基本职位:

    $premium_jobs = Posts::where('title', 'like', '%' . $request->q . '%')->where('type', 'op2')->where('type', 'basic')->where('status', 'active')->orderBy('id', 'desc')->take(3)->get();
    

    Example

  • 不显示基本职位,仅显示高级职位:

    $premium_jobs = $jobs->where('type', 'op2')->where('status', 'active')->paginate(15);
    

    Example

  • 显示高级工作和基本工作,但也将基本工作的副本显示为高级工作:

    $premium_jobs = $jobs->where('status', 'active')->paginate(15);
    

    Example


控制器:

<?php  

class SearchController extends Controller
{

    public function index(Request $request){

        $jobs = new Posts();
        if ($request->has('q')) {
            $jobs = $jobs->where('title', 'like', '%' . $request->q . '%');
        }

        if (!empty($request->city)) {
            $city = Cities::where('name', $request->city)->first();
            $jobs = $jobs->where('city_id', $city->id);
        }

        if (!empty($request->min_salary)) {
            $jobs = $jobs->where('salary', '>', $request->min_salary);
        }

        if (!empty($request->jt)) {
            $job_type = JobTypes::where('name', $request->jt)->first();
            $jobs = $jobs->where('job_type_id', $job_type->id);
        }

        if (!empty($request->ex)) {
            $jobs = $jobs->where('experience', $request->ex);
        }

        if (!empty($request->cat)) {
            $category = Categories::where('name', $request->cat)->first();
            $jobs = $jobs->where('cat_id', $category->id);
        }


// PREMIUM JOBS

       $premium_jobs = $jobs->where('status', 'active')->paginate(15);


// BASIC JOBS

        $basic_jobs = $jobs->where('type', 'basic')->where('status', 'active')->paginate(15);

        $cities = Cities::all();
        $job_types =  JobTypes::all();
        $categories =  Categories::all();

        return view('search.index', compact('basic_jobs', 'premium_jobs', 'request', 'cities', 'job_types', 'categories'));
    }

    ?>

任何见解将不胜感激!

提前致谢。

【问题讨论】:

  • 在查询中您必须使用orWhereThis Page 可以帮助我思考。

标签: php mysql laravel laravel-5


【解决方案1】:

您还有其他工作类型吗? 如果不试试这个

Posts::where('title', 'like', '%' . $request->q . '%')->where('status', 'active')->orderBy('id', 'desc')->take(3)->get();

【讨论】:

    【解决方案2】:
    <?php 
    
    // To get both the jobs : 
    
    $allJobs = $jobs->where('status', 'active')->paginate(15);
    
    // To get al basic jobs 
    
    $allBasicJobs = $jobs->where('status', 'active')->where('type', 'basic')->paginate(15);
    
    // To get all premium jobs 
    
    $allBasicJobs = $jobs->where('status', 'active')->where('type', 'op2')->paginate(15);
    
    // To search title in  basic jobs but get all premier jobs
    
    $filteredBasicJobsAllPremiumJobs = $jobs->where('status', 'active')
                                            ->where('type', 'op2')
                                            ->orWhere(function($q) use($request){
                                                return $q->where('type', 'basic')
                                                    ->where('title', 'like', '%' . $request->q . '%');
                                            })
                                            ->paginate(15);
    
    // To search title in  permium jobs but get all basic jobs
    
    $filteredPremiumJobsAllBasicJobs = $jobs->where('status', 'active')
                                            ->where('type', 'basic')
                                            ->orWhere(function($q) use($request){
                                                return $q->where('type', 'op2')
                                                    ->where('title', 'like', '%' . $request->q . '%');
                                            })
                                            ->paginate(15);
    
    // To search title in both basic and premium jobs
    
    $filteredJobs = $jobs->where('status', 'active')
                        ->where('title', 'like', '%' . $request->q . '%')
                        ->paginate(15);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多