sanu
sanu

Reputation: 1068

laravel 5.1 filter using pivot table

Hi i am developing job portal but facing a problem in pivot table filtering. i have a table like below

personal_details

id
name
sex
dob
nationality
visa_status

vacancies

id
name
description
created_at
updated_at

which have a many-to-many relation with pivot table. Data here is inserted when a job-seeker apply for a vacancy

personal_detail_vacancy

id
personal_detail_id
vacancy_id
created_at
updated_at

PersonalDetail model

class PersonalDetail extends Model
{

    public function vacancies()
    {

        return $this->belongsToMany('App\Vacancy')->withTimestamps();

 }
}

Vacancy model

class Vacancy extends Model
{
    public function personal_details()
    {

        return $this->belongsToMany('App\PersonalDetail');

}

what i want to do is select all personal detail who have applied for a job(in any vacancies) in a particular date
i tried

$personal_details = PersonalDetail::with(array('vacancies' => function($query){

       $query->wherePivot('created_at', '2015-11-10 11:33:24'); 

       }))->get();

but it is not filtering the date ANY IDEA?

Upvotes: 1

Views: 206

Answers (1)

sanu
sanu

Reputation: 1068

well solved this by using

$personal_details = PersonalDetail::whereHas('vacancies',function($query)
      {
             $query->where('created_at','2015-11-10 11:33:24'); 
      })->get();

it was not working before. The problem was i had 'created_at' field in vacancies table also.

Upvotes: 1

Related Questions