Tony
Tony

Reputation: 21

joining a table with 4 other table laravel eloquent

There are 5 tables like Doctor table having(id,name,city_id,country_id,insurance_id,speciality_id), city table(id,name,country_id), country table(id,name), insurance table(id,name), speciality table(id,name) and doctor_insurance table(id,doctor_id,insurance_id).

Relationship is as follows:-

doctor belongsTo City and City hasMany Doctor , doctor belongsTo speciality and speciality hasMany Doctor , City belongsTo Country and Country hasMany City , doctor belongsToMany insurance and insurance belongsTOMany Doctor.

How do I get the list of doctor where city=NewYork, Country=USA, Insurance=United Health Care and Speciality= Dentist?

Upvotes: 2

Views: 115

Answers (1)

Parth Patel
Parth Patel

Reputation: 521

you can query like this,

$result=DB::table("doctor as do")
->leftjoin('city  as ci', 'ci.id', '=', 'do.city_id')
->leftjoin("country as co","co.id","=","do.country_id")
->leftjoin("insurance as in","in.id","=","do.insurance_id")
->leftjoin("speciality as sp","sp.id","=","do.speciality_id")
->select('*')
->where('ci.name', '=', 'NewYork')
->where('co.name', '=', 'USA')
->where('in.name', '=', 'United Health Care')
->where('sp.name', '=', 'Dentist')
->get();

Upvotes: 2

Related Questions