Laravel 查询生成器 - 连接三个表(A 一对多 B,B 一对多 C)
Laravel Query Builder - Join three tables (A one to many B, B one to many C)
我有三张表。
国家:id,country_name
状态:id,country_id,state_name
城市:id,state_id,city_name
我想获取一个特定国家/地区($id = 1)以及每个州的所有城市。
我试试这个代码。
DB::table('country as a')
->join('state as b', 'b.country_id', '=', 'a.id')
->join('city as c', 'c.state_id', '=', 'b.id')
->select('a.*', 'b.*', 'c.*')
->where('a.id', '=', $id)
->get();
并得到这样的结果。
result: [
{id: 1, country_id: 1, country_name: ABC, state_name: aaa, state_id: 0, city_name: city a1},
{id: 2, country_id: 1, country_name: ABC, state_name: aaa, state_id: 0, city_name: city a2},
{id: 3, country_id: 1, country_name: ABC, state_name: aaa, state_id: 0, city_name: city a3},
{id: 4, country_id: 1, country_name: ABC, state_name: aaa, state_id: 0, city_name: city a4},
{id: 5, country_id: 1, country_name: ABC, state_name: bbb, state_id: 1, city_name: city b1},
{id: 6, country_id: 1, country_name: ABC, state_name: bbb, state_id: 1, city_name: city b2},
{id: 7, country_id: 1, country_name: ABC, state_name: bbb, state_id: 1, city_name: city b3},
{id: 8, country_id: 1, country_name: ABC, state_name: bbb, state_id: 1, city_name: city b4},
.....
]
希望能有这样的结果
result: [
{
id: 1,
country_id: 1,
country_name: ABC,
state_name: aaa,
state_id: 0,
city: [
{city_name: city a1},
{city_name: city a2},
{city_name: city a3},
{city_name: city a4}
]
},
{
id: 2,
country_id: 1,
country_name: ABC,
state_name: bbb,
state_id: 1,
city: [
{city_name: city b1},
{city_name: city b2},
{city_name: city b3},
{city_name: city b4}
]
},
.....
and like that for other states.
]
也许有人可以帮我提供一个新的见解。
非常感谢。
利用Eloquent关系
模型与关系
class Country extends Model
{
public function states()
{
return $this->hasMany(State::class);
}
}
class State extends Model
{
public function country()
{
return $this->belongsTo(Country::class);
}
public function cities()
{
return $this->hasMany(City::class);
}
}
class City extends Model
{
public function state()
{
return $this->belongTo(State::class);
}
}
查询
$countries = Country::query()
->with('states.cities')
->get();
我有三张表。
国家:id,country_name
状态:id,country_id,state_name
城市:id,state_id,city_name
我想获取一个特定国家/地区($id = 1)以及每个州的所有城市。
我试试这个代码。
DB::table('country as a')
->join('state as b', 'b.country_id', '=', 'a.id')
->join('city as c', 'c.state_id', '=', 'b.id')
->select('a.*', 'b.*', 'c.*')
->where('a.id', '=', $id)
->get();
并得到这样的结果。
result: [
{id: 1, country_id: 1, country_name: ABC, state_name: aaa, state_id: 0, city_name: city a1},
{id: 2, country_id: 1, country_name: ABC, state_name: aaa, state_id: 0, city_name: city a2},
{id: 3, country_id: 1, country_name: ABC, state_name: aaa, state_id: 0, city_name: city a3},
{id: 4, country_id: 1, country_name: ABC, state_name: aaa, state_id: 0, city_name: city a4},
{id: 5, country_id: 1, country_name: ABC, state_name: bbb, state_id: 1, city_name: city b1},
{id: 6, country_id: 1, country_name: ABC, state_name: bbb, state_id: 1, city_name: city b2},
{id: 7, country_id: 1, country_name: ABC, state_name: bbb, state_id: 1, city_name: city b3},
{id: 8, country_id: 1, country_name: ABC, state_name: bbb, state_id: 1, city_name: city b4},
.....
]
希望能有这样的结果
result: [
{
id: 1,
country_id: 1,
country_name: ABC,
state_name: aaa,
state_id: 0,
city: [
{city_name: city a1},
{city_name: city a2},
{city_name: city a3},
{city_name: city a4}
]
},
{
id: 2,
country_id: 1,
country_name: ABC,
state_name: bbb,
state_id: 1,
city: [
{city_name: city b1},
{city_name: city b2},
{city_name: city b3},
{city_name: city b4}
]
},
.....
and like that for other states.
]
也许有人可以帮我提供一个新的见解。
非常感谢。
利用Eloquent关系
模型与关系
class Country extends Model
{
public function states()
{
return $this->hasMany(State::class);
}
}
class State extends Model
{
public function country()
{
return $this->belongsTo(Country::class);
}
public function cities()
{
return $this->hasMany(City::class);
}
}
class City extends Model
{
public function state()
{
return $this->belongTo(State::class);
}
}
查询
$countries = Country::query()
->with('states.cities')
->get();