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

相關問題