小编sho*_*mes的帖子

Laravel:使用where子句查询和访问嵌套关系中的子对象

我试图访问嵌套关系的子对象,从父对象返回许多结果.

假设我有4个模型:国家 - 省 - 城市 - 市政

他们的关系如下:

国家模式

class Country extends Eloquent
{
   protected $table = 'countries';

   public function provinces()
   {
       return $this->hasMany('Province');
   }
}
Run Code Online (Sandbox Code Playgroud)

省模型

class Province extends Eloquent
{
   protected $table = 'provinces';

   public function cities()
   {
       return $this->hasMany('City');
   }
   public function country()
   {
       return $this->belongsTo('Country');
   }
}
Run Code Online (Sandbox Code Playgroud)

城市模型

class City extends Eloquent
{
   protected $table = 'cities';

   public function municipalities()
   {
       return $this->hasMany('Municipality');
   }
   public function province()
   {
       return $this->belongsTo('Province');
   }
}
Run Code Online (Sandbox Code Playgroud)

市政模式

class Municipality …
Run Code Online (Sandbox Code Playgroud)

nested relationships laravel eloquent

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

eloquent ×1

laravel ×1

nested ×1

relationships ×1