Check record if exist using updateOrCreate and do a math(SUM) if record exist in laravel 5.5

43
March 15, 2019, at 00:30 AM

I have a store function that saves array items into my items table and together with that I am trying to check if the product_is is already in my Warehouse1StockSummaries. if still not, I will grab the product_id and its qty, If its there already then I want to ADD the value from the 'stock_in_qty' which is inside the array to the 'qty_in' in my Warehouse1StockSummaries. I hope my explanation make sense to you :)

here's my code.

public function store(Request $request)
{
    $input = $request->all();
    $items = [];
    for($i=0; $i<= count($input['stock_in_qty']); $i++) {
        if(empty($input['stock_in_qty'][$i]) || !is_numeric($input['stock_in_qty'][$i])) continue;
        $acceptItem = [
            'order_id' => $input['order_id'][$i],
            'product_id' => $input['product_id'][$i],
            'order_item_id' => $input['order_item_id'][$i],
            'delivery_date' => $input['delivery_date'][$i],
            'company_id' => $input['company_id'][$i],
            'stock_in_qty' => intval($input['stock_in_qty'][$i]),
            'stock_out_qty' => $input['stock_out_qty'][$i],
            'transfer_to' => $input['transfer_to'][$i],
            'delivery_note' => $input['delivery_note'][$i],
            'user_id' => $input['user_id'][$i]  
        ];
        $product_id = $input['product_id'][$i];
        $qty_in = intval($input['stock_in_qty'][$i]);
        // dd($qty_in);

        // ADD stock_in_qty TO QTY_IN ????
        $stockSummary = Warehouse1StockSummaries::updateOrCreate(
            ['product_id' => $product_id ], 
            ['qty_in'     => $qty_in,
             'qty_out' => null
         ]);
        // dd($stockSummary);
        array_push($items, Warehouse1stocks::create($acceptItem));
    }
    return redirect()->route('orders.index');
}

I check and everything is ok the only missing is the part where I need to grab the value from 'stock_in_qty' and add to 'qty_in' if the product id is already found in Warehouse1StockSummaries. Thank you so much in advance!

Answer 1

You could use the wasRecentlyCreated property on the model to determine if the model has just been created or not. If it hasn't then it won't have used $qty_in value, this means you could then use the increment() to add to the existing value in the database:

$stockSummary = Warehouse1StockSummaries::firstOrCreate(
    ['product_id' => $product_id ], 
    ['qty_in' => $qty_in, 'qty_out' => null]
);
if (!$stockSummary->wasRecentlyCreated) {
    $stockSummary->increment('qty_in', $qty_in);
}
READ ALSO
crud edit button only showing last variable

crud edit button only showing last variable

I am creating my first php app and running into an issue on how I should try and edit a sql entryI was planning on trying to get a modal window to pop up in order to edit

27
String type casing in php, is it string or String?

String type casing in php, is it string or String?

What is the official name for the php string type?

18
add url parameters to $_GET variable with separated commas issue

add url parameters to $_GET variable with separated commas issue

This is my function, I simply want to output a link like this:

41
How to get current server avaible quota to send emails with PHP

How to get current server avaible quota to send emails with PHP

im in a development of an website that needs to send massive emails to their usersI have done the PHP script to send emails using PHPMailer and its ok, but the server has a limit by hour to send emails

45