[英]Multilingual site with one-to-many relationships in Eloquent
I have a problem with getting the relationships set up and queried correctly using Eloquent. 我有使用Eloquent正确建立和查询关系的问题。 I have products and each product can have multiple names/descriptions/prices based on a user's locale. 我有产品,并且每个产品可以根据用户的区域设置具有多个名称/说明/价格。 Now my problem is that when I try to use this relationship to update data pertaining to these products and I can only get either the product information (stock, id, date of manufacture etc) OR the language information but not both at the same time. 现在我的问题是,当我尝试使用这种关系来更新与这些产品有关的数据时,我只能获得产品信息(库存,编号,生产日期等)或语言信息,而不能同时获得两者。 I have been at this forever, going through the Eloquent documentation but the examples there are not very extensive. 我一直在研究Eloquent文档,但是那里的例子并不十分广泛。 Here are my models: 这是我的模型:
Model for the table holding the language specific information: 包含语言特定信息的表的模型:
class ProductLanguage extends BaseModel {
protected $table = 'products_lang';
protected $fillable = array('title','description','price','language');
public function product(){
return $this->belongsTo('Product');
}
}
Model for the product information: 产品型号信息:
class Product extends Eloquent {
protected $fillable = array('category_id', 'availability', 'image', 'year', 'stock');
public static $rules = array(
'category_id'=>'required|integer',
'title'=>'required|min:2',
'description'=>'required|min:20',
'price'=>'required|numeric',
'year'=>'required|integer',
'availability'=>'integer',
'image'=>'required|image|mimes:jpeg,jpg,bmp,png,gif'
);
public function category() {
return $this->belongsTo('Category');
}
public function productlanguage(){
return $this->hasMany('ProductLanguage');
}
}
I have tried pretty much all flavors described on the L4 documentation page to query this data so I have both sets (product + language) but it simply does not work. 我已经尝试了L4文档页面上描述的几乎所有口味来查询此数据,因此我同时拥有这两种设置(产品+语言),但是它根本无法正常工作。 Most of the time I get only the language data, sometimes I get an error saying unexpected data found but no matter what I do, I can't get the data I want. 大多数时候,我只获得语言数据,有时会收到一条错误消息,指出找到了意外数据,但是无论我做什么,我都无法获得想要的数据。 Is there a logic error here or what am I doing wrong? 这里是否存在逻辑错误,或者我在做什么错?
Update: Here are the queries I tried so far. 更新:这是我到目前为止尝试过的查询。
$product = Product::find($id)->productlanguage()->first();
However, this feels quite unspecific. 但是,这感觉很不确定。 I also have this one which gets me everything: 我也有一个可以让我得到一切的东西:
$products = Product::with('productlanguage')->get();
The tricky part is to get a product's language entry plus the matching product information. 棘手的部分是获取产品的语言条目以及匹配的产品信息。 I think I am gonna try this: 我想我要试试这个:
$products = ProductLanguage::with('product')->find($id);
I am still trying to wrap my head around this whole Eloquent thing. 我仍在努力围绕这整个雄辩的事情。 Thanks for your help so far. 感谢您一直以来的帮助。
It depends on what you exactly want. 这取决于您到底想要什么。
To get a Product with all of his productlanguage: 要获得具有所有产品语言的产品:
//This retrieves one product by id, and loads simultaneously the productlanguages
$product=Product::with('productlanguage')->find($id);
//This retrieves only the productlanguages as an Array
$productlanguages=$product->productlanguage;
or the other way around: 或相反:
$productLanguage=ProductLanguage::with('product')->find($id);
$product=$productLanguage->product;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.