在 Laravel 5.4 中将 Products/Quantity 添加到库存管理

Add Products/Quantity to Stock Management In Laravel 5.4

我正在使用 Laravel 5.4 开发库存系统。我需要帮助。我有产品 table 和库存 table。如果用户尝试将产品添加到库存中 supplier_id 和 product_id 已经存在,即(Select quantity FROM Stocks WHERE supplier_id=1 AND product_id=1) 应该将产品添加到现有库存的数量栏中,而不是将产品和数量插入到另一栏中。 即如果 Stock table 有 --> ProductName == Laptop;供应商ID==1;数量 ==(50)。如果用户 select ProductName == Laptop; AND SupplierID==1; Quantity Coulmn 应总和为 ( 50) 仅当 ProductName 和 Supplier 不存在于同一行时才插入,即 (Select quantity FROM Stocks WHERE supplier_id=20 AND product_id=2)。 我怎样才能有效地使用 Eloquent 来实现这个请 产品 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();
        });

现货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');
        });

我的库存控制器:;

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() !!}

希望有人能帮帮我。

您在 StockController 中的存储方法需要像这样:

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');
}

我认为这会解决问题,但请检查您对 unit_price 和 purchase_date 做了什么以防更新同一行?