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 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!