laravel雄辩的人际关系多

时间:2018-08-07 09:05:09

标签: laravel laravel-5

这是我的产品型号

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

这是我的订单模型

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

和我的订单表结构

enter image description here

这是我目前的回复(我正在使用Order模型)。

{
    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));

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

我想要的输出就是这样

{
    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并不陌生,而雄辩的关系是我经常混淆的部分,因为我感到困惑。但是现在我必须面对它。

2 个答案:

答案 0 :(得分:2)

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

  • 创建第一个表名称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)
        ];
    

答案 1 :(得分:1)

product_id表中删除quantityorders并创建一个表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

查看详细信息