【问题标题】:accessing the accessor from different model with many to many relationship laravel使用多对多关系从不同模型访问访问器 laravel
【发布时间】:2018-08-09 14:18:40
【问题描述】:

假设我有一个accessor 到我的Product 模型

class Product extends Model
{
    public function getIncomeAttribute() { 
        $sub_total = $this->price * $this->quantity; 
        $discount = round((10 / 100) * $sub_total, 2); 
        return round(($sub_total - $discount), 2); 
    }

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

我的产品型号与订单有多对多的关系,这是我的order model

class Order extends Model
{
    public function products(){
        return $this->belongsToMany(Product::class)->withPivot('quantity')->withTimestamps();
    }
}

如何从我的订单中访问我的产品模型中的访问器?我试过这个$this->products->quantity,但错误是Property [income] does not exist on this collection instance

这是我的OrderResource,它扩展了JsonResource,我尝试使用$this->products->income

public function toArray($request){
    $sub_total= 0;
    foreach($this->products as $product){
        $sub_total += ($product->pivot->quantity * $product->price);
    }
    $discount = round((10 / 100) * $sub_total, 2);
    $total = round(($sub_total - $discount), 2);
    $sub_total = round($sub_total,2);
    return [
        'id' => $this->id,
        'address' => $this->address,
        'sub_total' => $sub_total,
        'discount' => $discount,
        'total_price' => $this->products->quantity,
        'created_at' => Carbon::parse($this->created_at)->format('F d, Y h:i:s A'),
        'customer' => $this->user,
        'items' => ProductsResource::collection($this->products)
      ];
 }

【问题讨论】:

  • 你想在哪里访问它?
  • 在我的订单中,或者其他解决方案是在订单模型上制作访问器?
  • 是的,在订单模型中创建访问器。您可以使用$product->income 访问它,但在当前情况下不会给您正确的结果

标签: php laravel laravel-5


【解决方案1】:

在订单模型中创建subTotal 访问器

订单模式

public function getSubTotalAttribute() {  //calculate product subtotal 
   $sub_total = 0;

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

   return $sub_total;
}

现在在OrderResource使用它

公共函数 toArray($request){

$discount = round((10 / 100) * $this->subTotal, 2); 

$totalPrice = round(($this->subTotal - $discount), 2); 

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

}

注意:当您获取Order 时,立即加载Product

【讨论】:

  • 先生,我有一个问题,如何显示数据透视表?在我的订单中
  • 订单资源*
  • 为什么需要显示数据透视表,您可以使用ProductOrder 模型访问其所有属性。检查文档laravel.com/docs/5.6/eloquent-relationships#many-to-many
  • 你需要访问什么?
  • 在 OrderResource 中使用 $this->pivot 它将为您提供 order_product 表数据透视字段。例如你可以使用$this->pivot->quantity
猜你喜欢
  • 2020-05-20
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
  • 2017-08-07
相关资源
最近更新 更多