php - How can I query on 3 tables using has-many-through relationship in Laravel 5.4 -


i have searched many posts still confused. don't wanna use double query access info. i'm following example works on getting result without condition wanna apply condition on village table\model detailed below:

tables\models detail

  • province: id, name ....

  • district: id, name, province_id ....

  • village: id, name, status, district_id ....

edit

  1. province has many district. (one-to-many relationship)
  2. district has many villages. (one-to-many relationship)

here my question how province can access village info using has-many-through relationship on following criteria,

select village data district_id=some_value , status=some_value.

note: might suggest best.

thanks in advance

it assumed have these tables

 provinces   -id   -name  districts   -id   -name   -province_id  villages   -id   -name   -district_id    -status 
class province extends model {         public function villages(){         return $this->hasmanythrough('app\village','app\district');     } } 

you can query as

$province = app\province::find(1);         //assumed id of province 1 $province->villages;                       // gives collection of villages $villages->where('status','some_value')->where('district_id','some_district_val');   //filters collection 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -