1

I have three table below: I want to display all Related job post by category in Single jobpost. and I already have single job post page but in the same single job page I want to display related jobs in the left side. see my picture!

what is controller should be and in the Single job page (view) should be? please help?

Single job page

My jobController

 public function show($id, $company_id)
    {
        $singleJob = Job::find($id);
        $company = Company::find($company_id);
        $similarJob = Job::with('company')->where('category_id', $id)->get();
        return view('pages.single-job')->with([
            'singleJob'=> $singleJob,
            'company'=> $company,
            'similarJob' => $similarJob,
        ]);
    }

My relationship

job.php
public function company(){
    return $this->belongsTo(Company::class);
}

Job.php
public function category(){
return $this->belongsTo(Category::class);
}



//category.php
 public function job(){
        return $this->hasMany(Job::class);
    }


//company.php
    public function job(){
        return $this->hasMany(Job::class);
    }

Job table

    Schema::create('jobs', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('company_id');
        $table->string('jobTitle');
        $table->longText('jobDescription');

Company Table

Schema::create('company_types', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('admin_id');
            $table->string('name');
            $table->timestamps();
        });

Category table

 Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('name');
            $table->timestamps();
        });

1 Answers1

1

You can use whereHas like this :

public function show($id, $company_id)
{
    $singleJob = Job::find($id);
    $company = Company::find($company_id);
    $similarJobs = Job::with('company')
                       ->whereHas('category', function ($query) use($singleJob) {
                                $query->where('id', $singleJob->category->id);
                            })
                       ->get();
    return view('pages.single-job')->with([
        'singleJob'=> $singleJob,
        'company'=> $company,
        'similarJobs' => $similarJobs,
    ]);
}

And in the view you can use it like this :

@foreach ($similarJobs as $similarJob)
    // Add the job partial, componnent or just your HTML here for showing the Job
@endforeach

For the question in the comment, to find jobs that have a company that belongs to a given industry :

$some_industry_type_id = 1;
$jobsOfIndustryType = Job::whereHas('company', function ($query) use($some_industry_type_id) {
                                    $query->where('industry_type_id', $some_industry_type_id);
                                })
                           ->get();
Maraboc
  • 10,550
  • 3
  • 37
  • 48
  • Hello, Marabac, I have an other question. I have the same tables above and other table call industryType(id, name) which have relationship one to many with company table(id, comapnyName, ..,industry_type_id). I want to fetch all jobs that belong to each company that has it own industryType when click on industryType_id. – Chomneau Men Dec 07 '17 at 17:40
  • Happy to help ;) – Maraboc Dec 08 '17 at 14:37
  • Maraboc, I have other question. how can allow two auth to access one route. I have two auth "admin" and "employer" and I want this two auth can access to one route in order to update, create, job with the same permission. public function __construct() { $this->middleware('auth:admin'); // $this->middleware('auth:employer'); – Chomneau Men Jan 20 '18 at 16:09
  • Sorry i wasn't arround this this two days :p – Maraboc Jan 23 '18 at 00:26
  • Here is a [simple solution](https://stackoverflow.com/a/43902371/4881811) for your case :) – Maraboc Jan 23 '18 at 00:27