Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent update just if changes have been made

Is there any way to update a record in Laravel using eloquent models just if a change has been made to that record? I don't want any user requesting the database for no good reason over and over, just hitting the button to save changes. I have a javascript function that enables and disables the save button according with whether something has changed in the page, but I would like to know if it's possible to make sure to do this kind of feature on the server side too. I know I can accomplish it by myself (meaning: without appealing to an internal functionality of the framework) just by checking if the record has change, but before doing it that way, I would like to know if Laravel eloquent model already takes care of that, so I don't need to re-invent the wheel.

This is the way I use to update a record:

$product = Product::find($data["id"]);
$product->title = $data["title"];
$product->description = $data["description"];
$product->price = $data["price"];
//etc (string values were previously sanitized for xss attacks)
$product->save();
like image 699
user2755140 Avatar asked Mar 04 '15 22:03

user2755140


People also ask

How do you update a record in Laravel?

We can update the records using the DB facade with update method. The syntax of update method is as shown in the following table. Run an update statement against the database.

What is refresh in Laravel?

The refresh() method Laravel comes with a refresh() method, which when called on the model instance, will discard all the model attributes update and re-hydrate the existing model using fresh data from the database like so.

What is fillable in Laravel?

Fillable means what columns in the table are allowed to be inserted, guarded means the model can't insert to that particular column.

How to update user records in Laravel?

Actually Laravel makes this check itself, before saving it checks if any changes have been done by calling isDirty (), if it returns false, no update is made. Instead use update () to update the records... $user = Auth::user (); $userUpdated = $user->update ($request->all ()); //Print number of updated rows... print ($userUpdated);

How to update records if isdirty returns false in Laravel?

Besides isDirty (), you also have isClean (), and more. Actually Laravel makes this check itself, before saving it checks if any changes have been done by calling isDirty (), if it returns false, no update is made. Instead use update () to update the records...

What is eloquent in Laravel?

Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table.

Is it possible to get changes on the eloquent model?

You can use $product->getChanges () on Eloquent model even after persisting. Check docs here Show activity on this post. keep in mind that you have to name your inputs with the same column name.


3 Answers

You're already doing it!

save() will check if something in the model has changed. If it hasn't it won't run a db query.

Here's the relevant part of code in Illuminate\Database\Eloquent\Model@performUpdate:

protected function performUpdate(Builder $query, array $options = [])
{
    $dirty = $this->getDirty();

    if (count($dirty) > 0)
    {
        // runs update query
    }

    return true;
}

The getDirty() method simply compares the current attributes with a copy saved in original when the model is created. This is done in the syncOriginal() method:

public function __construct(array $attributes = array())
{
    $this->bootIfNotBooted();

    $this->syncOriginal();

    $this->fill($attributes);
}

public function syncOriginal()
{
    $this->original = $this->attributes;

    return $this;
}

If you want to check if the model is dirty just call isDirty():

if($product->isDirty()){
    // changes have been made
}

Or if you want to check a certain attribute:

if($product->isDirty('price')){
    // price has changed
}
like image 99
lukasgeiter Avatar answered Oct 16 '22 10:10

lukasgeiter


You can use $product->getChanges() on Eloquent model even after persisting. Check docs here

like image 39
Mladen Janjetovic Avatar answered Oct 16 '22 10:10

Mladen Janjetovic


I like to add this method, if you are using an edit form, you can use this code to save the changes in your update(Request $request, $id) function:

$post = Post::find($id);    
$post->fill($request->input())->save();

keep in mind that you have to name your inputs with the same column name. The fill() function will do all the work for you :)

like image 16
Ahmad Yousef Avatar answered Oct 16 '22 10:10

Ahmad Yousef