【问题标题】:laravel eloquent relationship multiplelaravel 雄辩的关系多重
【发布时间】:2018-08-07 09:05:08
【问题描述】:

这是我的产品型号

public function orders(){
    return $this->hasMany(Order::class);
}

这是我的订单模型

 public function user(){
      return $this->belongsTo(User::class);
 }
 public function product(){
      return $this->belongsTo(Product::class);
 }

还有我的订单表结构

这是我目前的回复(我使用的是订单模型)。

{
    id: 11,
    quantity: 2,
    customer: {
        id: 6,
        name: "saging",
        email: "saging@gmail.com",
        role_id: 3,
    },
    items: {
        id: 7,
        name: "Pellentesque semper",
        description: "raesent sit amet elementum purus. ",
        price: "72.21",
        cover_image: "7CPVS7yjqba9iJ7J.jpg",
    }
},

{
    id: 12,
    quantity: 2,
    customer: {
        id: 6,
        name: "saging",
        email: "saging@gmail.com",
        role_id: 3,
    },
    items: {
        id: 3,
        name: "Donec nec mattis ligula",
        description: "Pellentesque semper, augue at aliquet tincidunt",
        price: "33.21",
        cover_image: "ZJbbzjPwDXrcbkwi.jpg",
    }
 }

我目前使用的代码是

$products = Order::with('user','product');
return OrdersResource::collection($products->paginate(5));

我使用我的Order Model 中的方法列出products and customer

我想要的输出是这样的

{
    id: 12,
    quantity: 2,
    customer: {
        id: 6,
        name: "saging",
        email: "saging@gmail.com",
        role_id: 3,
    },
    items: [
        {
            id: 3,
            name: "Donec nec mattis ligula",
            description: "Pellentesque semper, augue at aliquet tincidunt",
            price: "33.21",
            cover_image: "ZJbbzjPwDXrcbkwi.jpg",
        },
        {
            id: 7,
            name: "Pellentesque semper",
            description: "raesent sit amet elementum purus. ",
            price: "72.21",
            cover_image: "7CPVS7yjqba9iJ7J.jpg",
        }
    ]
 }

这是我的 OrderResource 代码

$sub_total = $this->quantity * $this->product->price;
$discount = round((10 / 100) * $sub_total, 2);
$total = $sub_total - $discount;

return [
    'id' => $this->id,
    'quantity' => $this->quantity,
    'address' => $this->address,
    'sub_total' => $sub_total,
    'discount' => $discount,
    'total_price' => $total,
    'created_at' => Carbon::parse($this->created_at)->format('F d, Y h:i:s A'),
    'customer' => $this->user,
    'items' => $this->product,
    ];

我的关系有问题吗?我是 laravel 的新手,雄辩的关系是我总是跳过的部分,因为我很困惑。但现在我必须面对它。

【问题讨论】:

  • 我想你错过了这里的 ID id: 11, 是 12 不?请添加 OrdersResource 代码:)
  • 已编辑......
  • 有什么问题?您在 Order 和 Product 之间添加了 belongsTo 关系,那么它将始终是一个项目
  • 哦,我试过belongsToMany,但我也很困惑,因为我不知道什么是数据透视表我不知道从哪里开始哈哈
  • 我想要的是按顺序显示许多项目

标签: laravel laravel-5


【解决方案1】:

订单和产品之间的关系存在问题,您必须设置多对多关系,因为您需要:

  • 创建第三个表名order_product 应该包含order_idproduct_id

  • 在模型中设置关系:

    Product 型号:

    public function orders()
    {
        return $this->belongsToMany('App\Order');
    }
    

    Order 型号:

    public function products()
    {
        return $this->belongsToMany('App\Product');
    }
    
  • 最后你必须创建一个ProductResource 并且在OrderResource 你必须这样做:

    return [
        'id' => $this->id,
        'quantity' => $this->quantity,
        'address' => $this->address,
        'sub_total' => $sub_total,
        'discount' => $discount,
        'total_price' => $total,
        'created_at' => Carbon::parse($this->created_at)->format('F d, Y h:i:s A'),
        'customer' => $this->user,
        'items' => ProductResource::collection($this->products)
        ];
    

【讨论】:

  • 先生,我的订单表上我需要删除列 product_id 和 user_id 吗?
  • 忘了只删除 product_id 但将用于用户关系的 user_id 离开它;)
  • 好的,先生,我试试。但是 order_product 表与这两个模型有什么关系,因为我没有看到任何连接?谢谢先生
  • 您所要做的就是添加列 prder_id 和 product_id 并设置关系,laravel 将在幕后发挥作用;)
【解决方案2】:

orders 表中删除product_idquantity 并像这样创建一个表order_product

order_product(id, order_id, product_id, quantity)

订单模式

public function products()
{
    return $this->belongsToMany('App\Product')->withPivot('quantity')->withTimestamps();
}

产品型号

public function orders()
{
    return $this->belongsToMany('App\Order')->withPivot('quantity')->withTimestamps();
}

现在创建一个ProductResource

return [
    'id' => $this->id,
    'name' => $this->name,
    'description' => $this->description,
    'price' => $this->price,
 ];

现在更改 OrderResource

$sub_total= 0;
$quantity = 0;

foreach($this->products as $product){
  $sub_total += ($product->pivot->quantity * $product->price);
  $quantity += $product->pivot->quantity;
}

$discount = round((10 / 100) * $sub_total, 2);

$total = $sub_total - $discount;

return [
    'id' => $this->id,
    'quantity' => $quantity,
    'address' => $this->address,
    'sub_total' => $sub_total,
    'discount' => $discount,
    'total_price' => $total,
    'created_at' => Carbon::parse($this->created_at)->format('F d, Y h:i:s A'),
    'customer' => $this->user,
    'items' => ProductResource::collection($this->products),
    ];

创建订单后,像这样插入订单产品

$order->products()->attach([
    10 => ['quantity' => 1],
    22 => ['quantity' => 2]
]);

这里的 10 和 22 是产品 ID

在此处查看详细信息https://laravel.com/docs/5.6/eloquent-relationships#many-to-many

【讨论】:

  • 先生,之前我只在订单表上插入数据,但现在因为 order_product 表。我现在插入那 2 个表中的数据是否正确?
  • 该插入在哪个表中?是在订单表和 order_product 上吗?
  • 这将转到名为order_productpivot 表。从顶部阅读我的答案
  • 先生我像这样使用它 $table = OrderProduct::with('product');我用 $table->product()->attach($orders) 附加它;我得到一个错误查询生成器产品不存在
  • 为什么OrderProduct,只需创建一个没有product_idquantity 的订单,然后创建$order->products()->attach
猜你喜欢
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 2017-10-17
  • 2017-10-16
  • 2012-10-08
  • 2016-11-26
  • 2015-03-14
  • 2018-11-28
相关资源
最近更新 更多