php - Laravel 5.3: hasManyThrough relationship -


i have following situation:

location   id - integer   network_id - integer (fk)  network   id - integer   owner_id - integer (fk)  owner   id - integer   name - string 

i have eloquent model location data. want do, create restful api can retrieve owner data through network model.

i've tried using hasmanythrough no luck. model has following relationships;

network - location = 1 many owner - network = 1 one 

so many locations belong 1 network, , each network has 1 owner. case.

i've tried following.

class location extends model  {     public function owner() {        return $this->hasmanythrough('app\owner', 'app\network, 'id', 'id);     } } 

then return model in resource.

class locationcontroller extends controller  {    public function index() {       return [          'data' => location::with('network', 'owner')->take($limit)->take($offset)->get()       ];    } } 

i don't error model doesn't return owner, instead empty array.

can me out creating relationship between models using laravel's eloquent? i'm using laravel 5.3.

not sure whether table structure fits able use hasmanythrough

from can see documentation need

location   id - integer   network_id - integer  network   id - integer   location_id - integer  owner   id - integer   network_id - integer   name - string 

then use

 return $this->hasmanythrough(             'app\owner', 'app\network',             'location_id', 'network_id', 'id'         ); 

that being said may able work trying different combinations hasmanythrough e.g.

return $this->hasmanythrough('app\owner', 'app\network, 'owner_id', 'id', 'network_id'); 

Comments