简体   繁体   English

Laravel雄辩的ORM关系

[英]Laravel Eloquent ORM Relationship

This should be really simple, but I can't seem to figure it out. 这应该真的很简单,但是我似乎无法弄清楚。

There are two tables: 有两个表:

`images`
|-  `id`
|-  `path`
|-  `name`

`foods`
|-  `id`
|-  `image_id`

And two models: 和两个模型:

class Image extends Eloquent {

    public function food() {
        return $this->belongsToMany('Food');
    }

}

class Food extends Eloquent {

    public function image() {
        return $this->hasOne('Image');
    }

}

The idea is that each food has a single image associated with it through the image_id column in its foods table row. 这个想法是,每种food都有一个通过其foods表行中的image_id列与之关联的image A single image can be associated to multiple foods. 单个image可以与多种食物相关联。

I'd like to be able to do something like this in a controller: 我希望能够在控制器中执行以下操作:

$food = Food::with('image')->find(1);

...but, of course, I get the following error: ...但是,当然,我得到以下错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'images.food_id' in 'where clause' (SQL: select * from images where images . food_id in (1)) 未发现柱::SQLSTATE [42S22] 1054未知列'images.food_id'在'where子句'(SQL:SELECT * FROM images ,其中imagesfood_id中(1))

...because Eloquent would like each image to have a food_id . ...因为food_id希望每个image都有一个food_id But, of course, image s don't have food_id s. 但是,当然, image没有food_id Foods have image_id s. 食物有image_id

Please help a frustrated coder out. 请帮助沮丧的编码器。 :) :)

Thank you! 谢谢!


EDIT: 编辑:

Thank you so much, duellsy. 非常感谢,决斗。

I have updated my models to this: 我已经将模型更新为:

class Image extends Eloquent {

    public function food() {
        return $this->hasMany('Food');
    }

}

class Food extends Eloquent {

    public function image() {
        return $this->belongsTo('Image');
    }

}

...and now the Food::with('image') call works. ...现在, Food::with('image')调用起作用了。

I truly don't understand why 'Food' belongs to 'Image' though... perhaps someone might be able to explain why the syntax doesn't match the grammer here? 我确实不明白为什么“食物”属于“图像”……也许有人可以解释为什么语法与此处的语法不匹配?

While it grammatically sounds like it should be a hasOne relationship, the way your data is setup is actually a belongsTo relationship. 从语法上来说,它应该是hasOne关系,但数据的设置方法实际上是一个belongsTo关系。

ie, the food table has the foreign key, so it belongs to whatever that foreign key is pointing to 即,食物表具有外键,因此它belongs to外键指向的任何对象

Additionally, the inverse is true for the images model, it doesn't belong to anything since there's no foreign key or pivot tables involved. 此外,对于图像模型而言,逆是成立的,因为它不涉及外键或数据透视表,所以它不belong to任何东西。 Instead, it has many foods 相反,它has many食物

What's with the backwards sytax? 向后的语法是什么?

The grammar / syntax seems a bit backwards in your case purely because in most systems you would expect that a food item has many images, not the other way around. 在您的情况下,语法/语法似乎有些倒退,这纯粹是因为在大多数系统中,您期望食品具有许多图像,而不是相反。 It can get tricky often, which is why it's always worth forgetting about the model names and just thinking about who has the foreign keys. 它常常会变得棘手,这就是为什么总是值得忘记模型名称,而只是思考谁拥有外键的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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