问题描述
我有一个问题的理解很好的关系,让我们来点
I have an issue understanding pretty well the Relationships, let's go to the point
我有3个表,其适当的模型
I have 3 tables, with its proper models
receipt
- id
- date
items_sold
- id
- receipt_id
- item_id
items
- id
- name
我遇到的问题是收据有很多项目通过items_sold,但我不能使用hasManyThrough,因为雄辩使用错误item_sold的id,即使我手动键入
The problem I have is that receipt "has many" items through items_sold, but I can't use "hasManyThrough" because eloquent use the wrong "id" of "items_sold", even if I put manually the keys
class Receipt extends Eloquent {
public function items()
{
return $this->hasManyThrough('Items', 'ItemsSold', 'receipt_id', 'id');
}
}
没有办法通过这种方式,但我可以找到一个有说服力的关系方法,可以帮助我解决这个问题。
There is no way to do it in through this way, but I can find an Relationship method with eloquent that can help me with this case
推荐答案
正如lukasgeiter所说,添加到您的模型的 belongsToMany()
关系将允许您访问相关数据。
As lukasgeiter stated, a belongsToMany()
relationship added to your models would allow you to access the related data.
还有其他要注意的事项如果您将连接表的名称更改为 item_reciept
,则可以在模型中定义多对多关系,而无需专门指定连接表。当Laravel看到没有指定连接表名称的 belongsToMany
方法时,它会查找涉及Snake案例和小写名称的字母顺序的两个模型的名称。
Something else to note, if you change your join table's name to item_reciept
you can define the many to many relationship in your models without having to specify the join table specifically. When Laravel sees a belongsToMany
method without a join table name specified, it looks for the name of the two models involved in snake case and alphabetical order with lower case names.
您的模型方法将是:
class Receipt extends Eloquent{
public function items(){
return $this->belongsToMany('Item');
}
}
class Item extends Eloquent{
public function reciepts(){
return $this->belongsToMany('Reciept');
}
}
我可以理解,如果你不想重命名您的 items_sold
表,因为它的具体名称表示它只是一个连接表的使用。
I can understand if you wouldn't want to rename your items_sold
table because it's specific name denotes it's usage past just a join table.
另外需要注意的是,将这些关系添加到模型中,它允许您执行渴望加载您的请求,这可能对您的情况有帮助。
假设您想要一次性获取特定收据的所有项目。您可以使用以下请求来获取所有内容:
$receiptAndItems = Receipt::with('items')->find($recieptId);
这将返回您的特定收据记录和密钥项目的详细信息
与该给定收据的所有相关项目记录:
// Your `receipt` record
// this is the output when you add `->toArray()` at the end to make it a bit easier to read
array (size=7)
'id' => int 2
'name' => string 'Foo' (length=12)
'created_at' => string '2014-11-22 16:30:02' (length=19)
'updated_at' => string '2014-11-22 16:30:02' (length=19)
'items' =>
array (size=3)
0 =>
array (size=7)
'id' => int 1
'name' => string 'Bar' (length=13)
'created_at' => string '2014-11-22 16:30:02' (length=19)
'updated_at' => string '2014-11-22 16:30:02' (length=19)
'pivot' =>
array (size=2)
...
1 =>
array (size=7)
'id' => int 2
'name' => string 'Baz' (length=20)
'created_at' => string '2014-11-22 16:30:02' (length=19)
'updated_at' => string '2014-11-22 16:30:02' (length=19)
'pivot' =>
array (size=2)
...
2 =>
array (size=7)
'id' => int 3
'name' => string 'FooBarBaz' (length=18)
'created_at' => string '2014-11-22 16:30:02' (length=19)
'updated_at' => string '2014-11-22 16:30:02' (length=19)
'pivot' =>
array (size=2)
这篇关于查询关系Laravel雄辩ORM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!