简体   繁体   中英

Display information of 1 table from 2 columns in the the search results. (PHP - Laravel)

I have a webpage that lets you browse different job offers, these ones are saved in a MySQL table called “jobs”

“jobs” has different columns, one of them being “basic” for the normal jobs and “op2” for premium jobs.

When performing a search I'm trying to be able to display both of these categories on my results, however I only have only been able to display one, not both.

I was able to display both of these jobs using:


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

However when using the filters to search for specific words, it works with the basic jobs but displays all of the premium jobs without applying any filter.


Some queries that didn't worked:

  • Searches for basic jobs, but always displays all of the premium jobs no matter what the search was:

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

    Example

  • Doesn't display premium jobs, only displays basic jobs:

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

    Example

  • Doesn't display basic jobs, only displays premium jobs:

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

    Example

  • Displays premium jobs and basic jobs, but also shows a duplicate of the basic jobs as a premium one:

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

    Example


Controller:

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

    ?>

Any insight would be greatly appreciated!

Thanks in advance.

Do you have other job types?? If not try this

Posts::where('title', 'like', '%' . $request->q . '%')->where('status', 'active')->orderBy('id', 'desc')->take(3)->get();
<?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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM