Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save using condition in laravel?

Tags:

php

mysql

laravel

Basically I want to save data in my model using condition to prevent multiple save data being performed simultaneously.

What I currently do now is:

$order = Order::find(id);
$order->paid_amount = $amount;
$order->status = $status;
$order->save();

What I need is:

$order = Order::find(id);
$order->paid_amount = $amount;
$order->status = $status;
$order->where('last_update', $last_update);
$order->save();

Is it possible to do this in laravel efficiently or do I have to use raw sql or update?

like image 621
Nameless Avatar asked Dec 18 '15 10:12

Nameless


People also ask

How to use MySQL function in Laravel?

If you want to use mysql function then you must have to use db raw function. in this example i will show you laravel select query with if condition. In minor case we need to use if else condition in laravel 5, laravel 6, laravel 7 and laravel 8 Eloquent.

How to use if condition in Laravel SELECT query?

How to use if condition in laravel select query? We will learn how to use if condition in laravel select query. we can use mysql case when statement with db raw in laravel. If you want to use mysql function then you must have to use db raw function. in this example i will show you laravel select query with if condition.

How to update a saved record in Laravel database?

You can update the record which you recently saved in your database using save method in Laravel. You can get the id of recently saved record and after that you can use update method with where condition to update the record. Using this code snippet you can update the recently added record.


1 Answers

A simplified version of the @Amarnasan answer:

Order::findOrFail($id)->update([
     'paid_amount' => $amount,
     'status' => $status;
]);
like image 118
Pantelis Peslis Avatar answered Oct 11 '22 00:10

Pantelis Peslis