Laravel 5.2 Create function -


i've been building basic laravel crud system, i've made basic create funciton want make if record exists in database , u create same record again integer u put in create form add's existing record example: u have stock system product has ammount of 50, , u create same product ammount of 40, , 40 add's 50 in database 90. im not entirely sure how so.

this store function i've made application:

 public function store(request $request) {     // aantal = ammount (int)     //producten_id = foreign_key producten database table     //locaties_id = foreign_key locaties database table     voorraad::create($request->only(['aantal', 'producten_id', 'locaties_id']));     //var_dump($request);     return redirect(route('voorraad.index')); } 

if u need more information let me know , i'll add question

assuming set default value field in database structure, simple as:

$voorraad = voorraad::firstorcreate($request->only(['producten_id', 'locaties_id']);  $voorraad->increment('aantal', $request->aantal); 

if want set default amount new products in controller instead:

$voorraad = voorraad::firstorcreate($request->only(['producten_id', 'locaties_id']);  if (! $voorraad->aantal) {    $voorraad->aantal = 123; // default } else {    $voorraad->aantal += $request->aantal; }  $voorraad->save(); 

generally speaking firstorcreate method fetches database record passed attributes if exists or otherwise creates it. see in laravel docs.

if have questions ask in comments, i'll happy explain.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -