2017-06-16 78 views
0

我正在使用Laravel 5.4开发库存系统。我需要帮助。我有一个产品表和库存表。如果用户试图添加产品到库存,supplier_id和product_id已经存在,即(选择数量FROM库存,其中supplier_id = 1且product_id = 1)产品应添加到现有库存的数量列中,而不是插入将产品和数量放入另一栏。 即即使股票表具有 - >产品名称==笔记本电脑;供应商ID == 1;数量==(50)。如果用户选择ProductName == Laptop; AND SupplierID == 1;数量Coulmn应该总和为(50)只能在产品名称和供应商不存在于同一行时插入(即选择数量来自库存WHERE supplier_id = 20 AND product_id = 2)。 如何使用雄辩有效地实现这一PLS 产品表在Laravel 5.4中添加产品/数量到库存管理

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表

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'=> '[email protected]','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() !!} 

我希望有人能帮助我。

回答

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

+0

非常感谢!它解决了它......我将处理unit_price和purchase_date!再次感谢! – Steve

相关问题