我是拉维尔(Laravel)的新手。我正在开发一个项目。 我有4个表相互关联的
- 用户-ORDERS
-ORDERS
-orderParcels-
在这个项目中,
列出订单包裹时,我想仅获取该订单的信息一次将包裹列为其下方的表。到目前为止,一切正常。但是我也想显示表中列出的包裹的状态。我无法将第4个表添加到查询中。你有建议吗?我正在放置图片,以解释下面的结构。
$orderParcels = Orders::whereId($id)
->with('parcels')
->with('users:id,name')
->first();
public function parcels(){
return $this->hasMany(OrderParcels::class);
}
public function users(){
return $this->hasOne(User::class,'id','affixer_id');
}
$orderParcels = DB::table('order_parcels as op')
->leftjoin('orders as o','op.orders_id','o.id')
->leftjoin('users as u','o.affixer_id','u.id')
->leftjoin('situations as s','op.status','s.id')
->select('op.*','o.*','u.name','s.situations_name')
->where('op.orders_id',$id)->get();
对于每个包裹记录,它都会返回我的订单和用户信息。我想一次订购信息和一次用户信息。
I'm new to Laravel. I am developing a project. and in this project I have 4 tables related to each other
-Users
-Orders
-OrderParcels
-Situations
When listing the parcels of an order, I want to get the information of that order only once, the user information of that order once again, and list the parcels as a table under it. so far everything ok. but I also want to display the status of the parcels listed in the table as names. I couldn't add the 4th table to the query. do you have a suggestion? I'm putting pictures that explain the structure below.
My current working code is
$orderParcels = Orders::whereId($id)
->with('parcels')
->with('users:id,name')
->first();
and my 'orders' model has method
public function parcels(){
return $this->hasMany(OrderParcels::class);
}
public function users(){
return $this->hasOne(User::class,'id','affixer_id');
}
Note[edit]: I already know how to connect like this
$orderParcels = DB::table('order_parcels as op')
->leftjoin('orders as o','op.orders_id','o.id')
->leftjoin('users as u','o.affixer_id','u.id')
->leftjoin('situations as s','op.status','s.id')
->select('op.*','o.*','u.name','s.situations_name')
->where('op.orders_id',$id)->get();
but this is not working for me, for each parcels record it returns me orders and user info. I want once orders info and once user info.
发布评论
评论(2)
Laravel提供了一种优雅的方式来管理模型之间的关系。在您的情况下,第一步是创建模式中描述的所有关系:
1。模型顺序
2。模型包裹
然后,您可以简单地检索所有所需的信息:
它如何工作?
当您在模型上添加关系时,您可以通过使用属性来检索此关系的结果使用该方法的同名。 Laravel将自动为您提供这些属性! (例如:
parcels()
您的订单模型中的方法将生成$ order-> parcels
属性。要完成,在这种情况下,在您已经嵌套了关系的情况下(如在您的架构),您应该与模型的()方法一起使用,以急切地加载订单模型的所有嵌套关系:
我鼓励您阅读Laravel文档的存根:
祝你好运!
Laravel provides an elegant way to manage relations between models. In your situation, the first step is to create all relations described in your schema :
1. Model Order
2. Model Parcel
Then, you can retrieve all desired informations simply like this :
How it works ?
When you add a relation on your model, you can retrieve the result of this relation by using the property with the same name of the method. Laravel will automatically provide you those properties ! (e.g:
parcels()
method in your Order Model will generate$order->parcels
property.To finish, in this situation where you have nested relations (as describe in your schema), you should use with() method of your model to eager load all the nested relation of order model like this :
I encourage you to read those stubs of Laravel documentation :
Good luck !
使用加入以在桌子之间建立完美的关系。
Use join to make a perfect relations between tables.