簡體   English   中英

laravel api資源添加其他數據

[英]laravel api resource add other data

假設我有一個產品的API響應

{
    data: [
    {
        id: 3,
        name: "Test Product",
        price: "158.21",
        quantity: 4,
        income: 569.56
    },
    {
        id: 4,
        name: "Test Product",
        price: "58.21",
        quantity: 3,
        income: 157.17
    },
    ]
}

有什么辦法可以像這樣將我所有產品的總收入相加?

{
 data: [
    {
        id: 3,
        name: "Test Product",
        price: "158.21",
        quantity: 4,
        income: 569.56
    },
    {
        id: 4,
        name: "Test Product",
        price: "58.21",
        quantity: 3,
        income: 157.17
    },
    ],
    total: 726.73
}

這是我的類OrderProductResource ,它擴展了JsonResource

public function toArray($request)
    {
        $quantity = 0;
        $overAllTotal = 0;
        foreach($this->orders as $order){
            $quantity += $order->pivot->quantity; 
        }
        $sub_total = round($this->price * $quantity,2);
        $discount = round((10 / 100) * $sub_total, 2);
        $totalIncome = round(($sub_total - $discount), 2);
        return [
            'id' => $this->id,
            'name' => $this->name,
            'price' => $this->price,
            'quantity' => $quantity,
            'income' => $totalIncome,
        ];
    }

我試圖在laravel中使用with方法,但API響應仍然相同。

這是我的控制器

public function index(){
        $errorFound = false;
        $error = ['error' => 'No Results Found'];
        $products = Product::with('orders');
        if (request()->has('q')) {
            $keyword = '%'.request()->get('q').'%';
            $builder = $products->where('name', 'like', $keyword);
            $builder->count() ? $products = $builder : $errorFound = true;
        }
        return $errorFound === false ? OrderProductResourceCollection::collection($products->latest()->paginate()) : $error;
    }

您需要定義2 存取器quantityincome的產品型號

public function getQuantityAttribute() 
{ 
    $quantity = 0; 

    foreach($this->orders as $order){ 
       $quantity += $order->pivot->quantity; 
    } 

    return $quantity;
}

public function getIncomeAttribute()
{
    $sub_total = $this->price * $this->quantity;

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

    return round(($sub_total - $discount), 2); 
}

像這樣更改OrderProductResource

public function toArray($request)
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'price' => $this->price,
        'quantity' => $this->quantity,
        'income' => $this->income,
    ];
}

像這樣為OrderProductResource創建一個Resource集合類OrderProductResourceCollection

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class OrderProductResourceCollection extends ResourceCollection
{
    public function toArray($request)
    {
        return [
            'data' => $this->collection,
            'total' => $this->collection->sum('income')
        ];
    }
}

現在在控制器中像這樣使用它

$products = Product::with('orders')->get();
return response()->json(new OrderProductResourceCollection($products));

您可以在這里查看文檔以進行資源收集, 網址https://laravel.com/docs/5.6/eloquent-resources#concept-overview

嘗試這個:

public function toArray($request)
{
    $quantity = 0;
    $overAllTotal = 0;
    foreach($this->orders as $order){
        $quantity += $order->pivot->quantity; 
    }
    $sub_total = round($this->price * $quantity,2);
    $discount = round((10 / 100) * $sub_total, 2);
    $totalIncome = round(($sub_total - $discount), 2);
    return [
        'id' => $this->id,
        'name' => $this->name,
        'price' => $this->price,
        'quantity' => $quantity,
        'income' => $totalIncome,

        //your relation
        'realtion_name' => $this->relation_name
    ];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM