Laravel 查询生成器枢轴 table 连接
Laravel Query Builder pivot table connection
我正在尝试获取该国家/地区属于多对多关系区域的所有相关国家/地区。
我想在查询生成器关系中使用它
这个我转table
id bigint unsigned auto_increment
primary key,
country_id bigint unsigned not null,
region_id bigint unsigned not null
区域模型
public function r_countries()
{
return $this->belongsToMany(Country::class, "country_regions","region_id","country_id");
}
Country Model
public function r_regions(): BelongsToMany
{
return $this->belongsToMany(Region::class, "country_regions", "country_id", "region_id");
}
我把它绑起来了
$region = \App\Models\Country\Region::findOrFail(23);
$region->r_countries()->pluck("countries.title");
您可以使用
Model::whereHas('relation_name', function($query) use($varname){
$query->where(condition);
})->get()
我正在尝试获取该国家/地区属于多对多关系区域的所有相关国家/地区。 我想在查询生成器关系中使用它
这个我转table
id bigint unsigned auto_increment
primary key,
country_id bigint unsigned not null,
region_id bigint unsigned not null
区域模型
public function r_countries()
{
return $this->belongsToMany(Country::class, "country_regions","region_id","country_id");
}
Country Model
public function r_regions(): BelongsToMany
{
return $this->belongsToMany(Region::class, "country_regions", "country_id", "region_id");
}
我把它绑起来了
$region = \App\Models\Country\Region::findOrFail(23);
$region->r_countries()->pluck("countries.title");
您可以使用
Model::whereHas('relation_name', function($query) use($varname){
$query->where(condition);
})->get()