laravel 如何从多对多关系中仅获取活动元素与翻译
How to get only active element from many to many relationship with translation in laravel
我对多对多关系和术语的翻译有疑问。
我有 4 tables:
products
- id, price, whatever
products_lang
- id, product_id, lang, product_name
accessori
- id, active
accessori_lang
- id, accessori_id, lang, accessori_name
我正在尝试将配件分配给具有中间 table 的产品,名称为:
accessori_products
这是产品的型号:
class Product extends Model {
protected $table = 'products';
public function productsLang () {
return $this->hasMany('App\ProductLng', 'products_id')->where('lang','=',App::getLocale());
}
public function productsLangAll() {
return $this->hasMany('App\ProductLng', 'products_id');
}
public function accessori() {
return $this->belongsToMany('App\Accessori', 'accessori_products');
}
}
这是 productLng 的模型:
class ProductLng extends Model {
protected $table = 'products_lng';
public function products() {
return $this->belongsTo('App\Product', 'products_id', 'id');
}
}
然后我有配件的模型:
class Accessori extends Model {
protected $table = 'accessori';
public function accessoriLang() {
return $this->hasMany('App\AccessoriLng')->where('lang','=',App::getLocale());
}
public function accessoriLangAll() {
return $this->hasMany('App\AccessoriLng');
}
public function accessoriProducts() {
return $this->belongsToMany('App\Products', 'accessori_products', 'accessori_id', 'products_id');
}
}
以及 AccessoriLng 的型号:
class accessoriLng extends Model {
protected $table = 'accessori_lng';
public function accessori() {
return $this->belongsTo('App\Accessori', 'accessori_id', 'id');
}
}
我通过这个得到结果:
$products = Product::has('accessori')->with([
'productsLang ',
'accessori' => function ($accessori){
$accessori->with([
'accessoriLang'
]);
}
])->get();
return $products;
但是我只想得到像where accessori.active = 1
这样的活动配件,但我真的不知道把它放在哪里。我尝试过不同的方式,但坚持了 2 天。
IIRC 你不需要中间模型 table 你的多对多关系。
如果您想要 return 激活配件的产品,您可以在产品型号上使用 whereHas
。
$prod = Product::whereHas('accessori', function($query) {
$query->where('active', 1);
})->get();
其中 $query
参数在 Accessori 模型上将是 运行。
您也可以使用 Accessori to Product 执行相反的操作。
$acessoris = Accessori::where('active', 1)->whereHas('accessoriProduct')->with(['accessoriLang', 'accessoriProducts.productsLang'])->get();
我对多对多关系和术语的翻译有疑问。 我有 4 tables:
products
- id, price, whatever
products_lang
- id, product_id, lang, product_name
accessori
- id, active
accessori_lang
- id, accessori_id, lang, accessori_name
我正在尝试将配件分配给具有中间 table 的产品,名称为:
accessori_products
这是产品的型号:
class Product extends Model {
protected $table = 'products';
public function productsLang () {
return $this->hasMany('App\ProductLng', 'products_id')->where('lang','=',App::getLocale());
}
public function productsLangAll() {
return $this->hasMany('App\ProductLng', 'products_id');
}
public function accessori() {
return $this->belongsToMany('App\Accessori', 'accessori_products');
}
}
这是 productLng 的模型:
class ProductLng extends Model {
protected $table = 'products_lng';
public function products() {
return $this->belongsTo('App\Product', 'products_id', 'id');
}
}
然后我有配件的模型:
class Accessori extends Model {
protected $table = 'accessori';
public function accessoriLang() {
return $this->hasMany('App\AccessoriLng')->where('lang','=',App::getLocale());
}
public function accessoriLangAll() {
return $this->hasMany('App\AccessoriLng');
}
public function accessoriProducts() {
return $this->belongsToMany('App\Products', 'accessori_products', 'accessori_id', 'products_id');
}
}
以及 AccessoriLng 的型号:
class accessoriLng extends Model {
protected $table = 'accessori_lng';
public function accessori() {
return $this->belongsTo('App\Accessori', 'accessori_id', 'id');
}
}
我通过这个得到结果:
$products = Product::has('accessori')->with([
'productsLang ',
'accessori' => function ($accessori){
$accessori->with([
'accessoriLang'
]);
}
])->get();
return $products;
但是我只想得到像where accessori.active = 1
这样的活动配件,但我真的不知道把它放在哪里。我尝试过不同的方式,但坚持了 2 天。
IIRC 你不需要中间模型 table 你的多对多关系。
如果您想要 return 激活配件的产品,您可以在产品型号上使用 whereHas
。
$prod = Product::whereHas('accessori', function($query) {
$query->where('active', 1);
})->get();
其中 $query
参数在 Accessori 模型上将是 运行。
您也可以使用 Accessori to Product 执行相反的操作。
$acessoris = Accessori::where('active', 1)->whereHas('accessoriProduct')->with(['accessoriLang', 'accessoriProducts.productsLang'])->get();