I'm working on a project in Laravel 6.2. I've two models.
- Job
- Company
What i want to do is to get all the jobs where the job title matches something and the company city is equal to something. The problem is, it's getting all the job with the companies but if i specify the wrong company city, it still gets the records. In short the condition inside the with not working. What i've done so far:
Model > Job.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Job extends Model
{
// user
public function user()
{
return $this->belongsTo('App\User');
}
// company
public function company()
{
return $this->belongsTo('App\Company');
}
}
Model > Company.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
// user
public function user()
{
return $this->belongsTo('App\User');
}
// job
public function job()
{
return $this->hasMany('App\Job');
}
}
JobController > search
public function search(Request $request)
{
$jobs = Job::where('title', 'like', '%' . $request->q . '%')
->with(['company' => function($q) use ($request) {
$q->where('city', $request->city);
}])
->get();
return view('jobs.view')->withJobs($jobs);
}