samedi 2 juillet 2016

How can we make in laravel multilevel association

I have 4 tables. I want to implement query on one table and getdata related tables. Please help me how can I do this. In cakphp we use contain but laravel I have no idea.

countries, states, cities, locations

Model code
Country Model

    class Country extends Model {
        public function states() {
            return $this->hasMany('AppState');
        }
    }

    class State extends Model {
        public function city() {
            return $this->hasMany('AppCity');
        }
    }


    class City extends Model {
        public function location() {
            return $this->hasMany('AppLocation');
        }
    }
    class Location extends Model {

    }

I have make query on country and get state

$country = Country::where('id',1);
$country->states

like that but how can I get cities -> location with this. May make another query manually? In cakephp we use contain, In lavavel no such these type of keyword in this?

Please help me

Aucun commentaire:

Enregistrer un commentaire