2

i've been building a basic laravel CRUD system, with that i've made a basic create funciton but i want to make it so that if a record already exists in the database and u create the same record again that the Integer u put in the create form add's up by the already existing record example: u have a stock system with a product that has the ammount of 50, and u create the same product with the ammount of 40, and that the 40 add's up with the 50 so that in the database it will say 90. im not entirely sure how to do so.

This is the store function i've made for my application:

 public function store(Request $request)
{
    // Aantal = ammount (int)
    //Producten_Id = foreign_key from the Producten database table
    //Locaties_Id = foreign_key from the 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 just let me know and i'll add it to the question

2
  • It should be coming with your database. not laravel. Commented Nov 17, 2016 at 15:18
  • You will need to perform checks to see if the record already exists and if it does then update the record forexample update the value 40 with the new value 60 but dont delete the 40 instead add 40 to 60 to make 100 then update the value so the new value will say 100, Correct? Commented Nov 17, 2016 at 15:20

1 Answer 1

1

Assuming that you set up default value for this field in database structure, its as simple as:

$voorraad = Voorraad::firstOrCreate($request->only(['Producten_Id', 'Locaties_Id']);

$voorraad->increment('aantal', $request->aantal);

If you want to set default amount for new products in the controller do this 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 from database record with passed attributes if it exists already or otherwise creates it. See in Laravel docs.

If you have any questions just ask in comments, I'll be happy to explain.

Sign up to request clarification or add additional context in comments.

3 Comments

i've tried both sets of code and it doesn't achieve what i want it to, i want to, if i have a product in my database with a amount with it, that when i add the same product the ammout that i enter the second time is adding up with the ammount of the product in the database, and with the first line of code after i've added 1 product and im trying to add another one the total amount doesn't change,
This is exactly what code I posted is supposed to do. How do you say that the product is the same? Just by Producten_Id? Then remove Locaties_Id from firstOrCreate. As a side note, it's a good idea to use English names for objects, attributes, methods etc in your code. Or at least translate them before posting cause now I'm kinda guessing what your code is all about ;) Also take a look at laravel.com/docs/5.3/eloquent#other-creation-methods to understand what we achieve with firstOrCreate.
thank you for your help, the link helped me out a ton

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.