在Laravel 5.4中添加产品/数量到库存管理

时间:2017-06-16 12:08:01

标签: laravel laravel-5 eloquent laravel-eloquent

我正在使用Laravel 5.4开发库存系统。我需要帮助。我有一个产品表和库存表。如果用户尝试将产品添加到库存,其中supplier_id和product_id已经存在,即(选择数量FROM Stocks WHERE supplier_id = 1 AND product_id = 1)产品应添加到数量列中现有库存而不是将产品和数量插入另一列。 即如果Stock表有 - > ProductName ==笔记本电脑;供应商ID == 1;数量==(50)。如果用户选择ProductName == Laptop; AND SupplierID == 1;数量库存应为(50)插入应仅在ProductName和供应商不存在于同一行时,即(选择数量FROM Stocks WHERE supplier_id = 20 AND product_id = 2)。 我怎样才能有效地使用Eloquent实现这一目标  产品表

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

股票表

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

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

我希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

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做什么?