vendredi 15 février 2019

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.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2BE8kPP
via IFTTT

Aucun commentaire:

Enregistrer un commentaire