Laravel 查询关系LAVEL雄辩ORM
我有一个问题,很好地理解了这些关系,让我们开门见山吧 我有三张桌子,都有合适的型号 我所遇到的问题是,收据上“有许多”已售出的物品,但我不能使用“hasManyThrough”,因为即使我手动输入钥匙,也会使用“已售出物品”的错误“id”Laravel 查询关系LAVEL雄辩ORM,laravel,laravel-4,eloquent,relationship,Laravel,Laravel 4,Eloquent,Relationship,我有一个问题,很好地理解了这些关系,让我们开门见山吧 我有三张桌子,都有合适的型号 我所遇到的问题是,收据上“有许多”已售出的物品,但我不能使用“hasManyThrough”,因为即使我手动输入钥匙,也会使用“已售出物品”的错误“id” class Receipt extends Eloquent { public function items() { return $this->hasManyThrough('Items', 'ItemsSold',
class Receipt extends Eloquent {
public function items()
{
return $this->hasManyThrough('Items', 'ItemsSold', 'receipt_id', 'id');
}
}
没有办法通过这种方式做到这一点,但我可以找到一种有说服力的关系方法,可以帮助我解决这个问题我很确定你真正需要的是找到一种多对多的关系
你的模特真的叫“物品”吗?如果它实际上是“Item”,则相应地更改为上述代码中的第一个参数,正如lukasgeiter所述,添加到模型中的
belongTomany()
关系将允许您访问相关数据
另外需要注意的是,如果将联接表的名称更改为item_receipt
,则可以在模型中定义多对多关系,而无需专门指定联接表。当Laravel看到一个没有指定联接表名称的belongToMany
方法时,它会查找两个模型的名称,这两个模型的名称都是以蛇形大小写的,并按字母顺序以小写的名称排列
您的模型方法将是:
class Receipt extends Eloquent{
public function items(){
return $this->belongsToMany('Item');
}
}
class Item extends Eloquent{
public function reciepts(){
return $this->belongsToMany('Reciept');
}
}
如果您不想重命名您的items\u salled
表,我可以理解,因为它的特定名称表示它的用法超过了联接表
关于将这些关系添加到模型中,另一件需要注意的事情是,它允许您处理您的请求,这在您的情况下可能会有所帮助
假设您希望通过id一次性获取特定收据的所有项目。您可以使用以下请求将所有内容整合在一起:
$receiptAndItems = Receipt::with('items')->find($recieptId);
这将返回特定收据记录上的详细信息和一个键items
,以及给定收据的所有相关项目记录:
// 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)
选择
项目
*,项目
收据
从项目
内部连接项目
在项目
id
项目id“其中项目
收据=1这是雄辩的陈述,除了这部分“items\u salled
id
=items
id
”应该是“items\u salled
items\u iditemsid
”它的真名是另一个呵呵,我犯了一个错误,但正如你说的“item”,我要去做一些测试,谢谢你。好吧,你的权利,谢谢,我需要拉威尔假人手册(无问题:)如果您的问题得到解决,请接受我的回答无问题。我不想骗你的钱,但我只是在我目前的项目中花了很多时间才弄明白,所以我想把钱提前支付:)不用担心。我相信总有一天会有人欣赏它:)
$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)