douqi1625 2014-01-25 21:38
浏览 51
已采纳

在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.

  • 写回答

1条回答 默认 最新

  • duanchi1230 2014-01-26 09:19
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn