拉拉维尔(Laravel)雄辩的四张桌子

发布于 2025-01-22 23:20:41 字数 1292 浏览 0 评论 0 原文

我是拉维尔(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.

enter image description here

enter image description here

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小猫一只 2025-01-29 23:20:41

Laravel提供了一种优雅的方式来管理模型之间的关系。在您的情况下,第一步是创建模式中描述的所有关系:

1。模型顺序

class User extends Model {
    public function parcels()
    {
        return $this->hasMany(OrderParcels::class);
    }

    public function users()
    {
        return $this->hasOne(User::class,'id','affixer_id');
    }
}

2。模型包裹

class Parcel extends Model {
    public function situations()
    {
        return $this->hasOne(Situation::class, ...);
    }
}

然后,您可以简单地检索所有所需的信息:

// Retrieve all users of an order 
$users = $order->users; // You get a Collection of User instances

// Retrieve all parcels of an order 
$parcels = $order->parcels; // You get a Collection of User instances

// Retrieve the situation for a parcel
$situations = $parcel->situations // You get Situation instance

它如何工作?

当您在模型上添加关系时,您可以通过使用属性来检索此关系的结果使用该方法的同名。 Laravel将自动为您提供这些属性! (例如: parcels()您的订单模型中的方法将生成 $ order-> parcels 属性。

要完成,在这种情况下,在您已经嵌套了关系的情况下(如在您的架构),您应该与模型的()方法一起使用,以急切地加载订单模型的所有嵌套关系:

$orders = Orders::with(['users', 'parcels', 'parcels.situations'])->find($id)

我鼓励您阅读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

class User extends Model {
    public function parcels()
    {
        return $this->hasMany(OrderParcels::class);
    }

    public function users()
    {
        return $this->hasOne(User::class,'id','affixer_id');
    }
}

2. Model Parcel

class Parcel extends Model {
    public function situations()
    {
        return $this->hasOne(Situation::class, ...);
    }
}

Then, you can retrieve all desired informations simply like this :

// Retrieve all users of an order 
$users = $order->users; // You get a Collection of User instances

// Retrieve all parcels of an order 
$parcels = $order->parcels; // You get a Collection of User instances

// Retrieve the situation for a parcel
$situations = $parcel->situations // You get Situation instance

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 :

$orders = Orders::with(['users', 'parcels', 'parcels.situations'])->find($id)

I encourage you to read those stubs of Laravel documentation :

Good luck !

聽兲甴掵 2025-01-29 23:20:41

使用加入以在桌子之间建立完美的关系。

$output = Orders::join('users', 'users.id', '=', 'orders.user_id')
->join('order_parcels', 'order_parcels.id', '=', 'orders.parcel_id')
->join('situations', 'situation.id', '=', 'order_parcels.situation_id')
->select([
'orders.id AS order_id',
'users.id AS user_id',
'order.parcels.id AS parcel_id',
'and so on'
])
->where('some row', '=', 'some row or variable')->get();

Use join to make a perfect relations between tables.

$output = Orders::join('users', 'users.id', '=', 'orders.user_id')
->join('order_parcels', 'order_parcels.id', '=', 'orders.parcel_id')
->join('situations', 'situation.id', '=', 'order_parcels.situation_id')
->select([
'orders.id AS order_id',
'users.id AS user_id',
'order.parcels.id AS parcel_id',
'and so on'
])
->where('some row', '=', 'some row or variable')->get();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文