Laravel Updates restock from 0 to 1 in database if stock is equal to minimum stock

51 views Asked by At

How do I automatically update the restock value if my stock is the same as the minimum stock then the value is 1 and if not the value is 0? What section is the code logic written in?

I use Laravel 10

This is the migration database code

Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('product_name');
            $table->string('category_id');
            $table->string('unit_id');
            $table->string('product_code')->nullable();
            $table->double('min_stock');
            $table->double('stock');
            $table->integer('restock')->default(0)->comment('0=No, 1=Yes');
            $table->string('product_image')->nullable();
            $table->timestamps();
        });

This is the controller code if there is stock out

public function updateOrder(Request $request)
    {
        $order_id = $request->id;

        // Reduce the stock
        $products = OrderDetails::where('order_id', $order_id)->get();

        foreach ($products as $product) {
            Product::where('id', $product->product_id)
                    ->update(['stock' => DB::raw('stock-'.$product->quantity)]);
        }

        Order::findOrFail($order_id)->update(['order_status' => 'complete']);

        return Redirect::route('order.completeOrders')->with('success', 'Order has been completed!');
    }

This is the controller code if there is incoming stock

public function updatePurchase(Request $request)
    {
        $purchase_id = $request->id;

        // after purchase approved, add stock product
        $products = PurchaseDetails::where('purchase_id', $purchase_id)->get();

        foreach ($products as $product) {
            Product::where('id', $product->product_id)
                    ->update(['stock' => DB::raw('stock+'.$product->quantity)]);
        }

        Purchase::findOrFail($purchase_id)
            ->update([
                'purchase_status' => 1,
                'updated_by' => auth()->user()->id
            ]); // 1 = approved, 0 = pending

        return Redirect::route('purchases.allPurchases')->with('success', 'Purchase has been approved!');
    }

I haven't tried it because I'm still confused

0

There are 0 answers