0
votes

I'm developing Inventory System Using Laravel 5.4. I need help. I've a product table and stock table. If the user tried to add a product to stock whereby the supplier_id and product_id already exist i.e (Select quantity FROM Stocks WHERE supplier_id=1 AND product_id=1) The product should be added to the quantity column of the existing stock instead of inserting the product and quantity into another column. i.e if Stock table has --> ProductName == Laptop; SupplierID==1; Quantity ==(50). If the user select ProductName == Laptop; AND SupplierID==1; The Quantity Coulmn should be sum to ( 50) Inserting should ONLY be when ProductName and Supplier doesn't exist in same row i.e (Select quantity FROM Stocks WHERE supplier_id=20 AND product_id=2) . How can I use Eloquent effectively to achieve this pls Product Table

Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('brand_id')->index()->unsigned()->nullable();
            $table->string('name');
            $table->string('part_number');
            $table->string('serial_number');
            $table->timestamps();
        });

STOCK TABLE

Schema::create('stocks', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('product_id')->index()->unsigned()->nullable();
            $table->integer('category_id')->index()->unsigned()->nullable();
            $table->integer('supplier_id')->index()->unsigned()->nullable();
            $table->string('quantity');
            $table->string('unit_price');
            $table->date('purchased_date');
            $table->timestamps();
            $table->date('delete_at');
        });

My StockController :;

public function create(Request $request)
{
   $products= Product::lists('name', 'id')->all();
    $categories= Category::lists('name', 'id')->all();
    $suppliers= Supplier::lists('name', 'id')->all();     
    return view('admin.stocks.create', compact('products','categories','suppliers'));
}
public function store(Request $request)
{
    Stock::create($request->all());
    return redirect('/admin/stocks');
}

create.blade.php

{!! Form::open(['method'=>'POST', 'action'=> 'StocksController@store','files'=>true]) !!}
<div class="form-group">
    {!! Form::label('supplier_id', 'Supplier/Vendor:') !!}
    {!! Form::select('supplier_id', [''=>'Select Supplier'] + $suppliers, null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
    {!! Form::label('product_id', 'Part Name:') !!}
    {!! Form::select('product_id', [''=>'Select Part Name'] + $products, null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
    {!! Form::label('category_id', 'Category:') !!}
    {!! Form::select('category_id', [''=>'Choose Category'] + $categories, null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
    {!! Form::label('quantity', 'Quantity:') !!}
    {!! Form::text('quantity', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
    {!! Form::label('purchased_date', 'Purchased Date:') !!}
    {!! Form::text('purchased_date', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
    {!! Form::label('unit_price', 'Unit Price (Naira):') !!}
    {!! Form::text('unit_price', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
    {!! Form::submit('Add Stock', ['class'=>'btn btn-primary']) !!}
</div>

{!! Form::close() !!}

I hope someone can help me out.

1

1 Answers

1
votes

Your store method in StockController need to be like this:

public function store(Request $request)
{
    $stock = Stock::where([
        ['product_id', '=', $request->product_id],
        ['supplier_id', '=', $request->supplier_id]
    ])->first();

    if ($stock) {
        $stock->increment('quantity', $request->quantity);
    } else {
        Stock::create($request->all());
    }
    return redirect('/admin/stocks');
}

Think this will fix problem, but check what you do with unit_price and purchase_date in case of updating same row?