从具有多对多关系的不同模型访问访问器 [英] accessing the accessor from different model with many to many relationship laravel

查看:49
本文介绍了从具有多对多关系的不同模型访问访问器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的Product模型有一个accessor

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

My product model has a many to many relationship with order, this is my 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

from my order how can i access the accessor in my product model? i tried this $this->products->quantity, but the error is Property [income] does not exist on this collection instance

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

this is my OrderResource that extends JsonResource where i tried to used the $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)
      ];
 }

推荐答案

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

Create subTotal accessor in Order Model

订单模型

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

public function 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

Note: When you fetch Order then eager load Product

这篇关于从具有多对多关系的不同模型访问访问器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆