ntn0de
4 months ago
707
4
Laravel

HELP,TO MAKE IT WORK.

Posted 4 months ago by ntn0de

Hello everyone I really Need a help optimizing and make this thing work.

Below is the controller that receives data from mobile. data in a structure :

"products": [
{
'id': 12,
'title': "abcd ",
'quantity': 2,
'price': 123,
},
{
'id': 14,
'title': "xyz ",
'quantity': 3,
'price': 123,
},
],

i would like to save it each to orderedProduct model and increase the total sold in Product' table too.a I think i m doing a lot of queries. and i worry the atomicity isn't managed well.

AND ALSO PRODUCT isn't updating as well

<?php
namespace App\Http\Controllers\Api;

use App\Http\Resources\API\Order\OrderCollection;
use App\Http\Controllers\Controller;
use App\Http\Resources\API\Order\OrderResource;
use App\Model\Order;
use App\Model\OrderedProduct;
use App\Model\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Spatie\QueryBuilder\QueryBuilder;

class OrderController extends Controller
{

    public function index()
    {
        $orders = QueryBuilder::for(Order::class)
            ->allowedFilters(['consumer_id'])
            ->allowedSorts('id')
            ->latest()
            ->get();

        return OrderCollection::collection(Order::paginate(20));
    }
    public function store(Request $request)
    {

        $user = Auth::user();
        $order = new Order;

        $order->consumer_id = $user->id;
        $order->billing_email = $user->email;
        $order->billing_name = $user->name;
        $order->billing_address = 'user address';
        $order->billing_city = 'city';
        $order->billing_province = 'province';
        $order->billing_postalcode = 0;
        $order->billing_phone = $user->phone;
        $order->billing_name_on_card = $user->name;
        $order->billing_discount = 0;
        $order->billing_discount_code = 'None';
        $order->billing_subtotal = 0.0;
        $order->billing_tax = 0.0;
        $order->billing_total = 0.0;
        $order->payment_gateway = 'cash_on_delivery';
        $order->error = 'none';

        $order->save();
        $tsum = 0;
        foreach ($request['products'] as $op) {
            $item = $this->orderedProductSave($order, $op);
            $tsum += $item;
        }
        $order->update(array('billing_subtotal' => $tsum, 'billing_total' => $tsum));

        return json_encode($request->amount);
    }
    public function orderedProductSave(Order $order, $op)
    {
        $product = Product::find($op['id']);
        $orderedProduct = new OrderedProduct();
        $orderedProduct->product_id = $op['id'];
        $orderedProduct->product_name = $op['title'];
        $orderedProduct->per_price = $product->selling_price;
        $orderedProduct->total_quantity = $op['quantity'];
        $tprice = $op['quantity'] * $op['price'];
        $orderedProduct->total_price =  $tprice;
        $order->orderedproducts()->save($orderedProduct);
        $total_quantity = $product->total_sold + $op['quantity'];
        $product->update(array('total_sold' => $total_quantity));
        return $tprice;
    }

    public function show(Order $order)
    {
        return new OrderResource($order);
    }

    public function update(Request $request, Order $order)
    {
        //
    }
}

Please sign in or create an account to participate in this conversation.