vendredi 1 juillet 2016

How can we make a laravel multilevel association

I have 4 tables. I want to implement a query on one table and get data from related tables.

In CakePHP, we use contain but in Laravel I have no idea.

  • countries,
  • states,
  • cities,
  • locations

Model Code

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 to make a query on Country and I want to also retrieve the State

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

Like that, but how can I get cities -> location with this. Do I need to make another query manually? In CakePHP we use contain, but in Laravel, there is no similar keyword or functionality to this?

Aucun commentaire:

Enregistrer un commentaire