简体   繁体   中英

Multilingual site with one-to-many relationships in Eloquent

I have a problem with getting the relationships set up and queried correctly using 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. 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. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM