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();
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.
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.
Fillable means what columns in the table are allowed to be inserted, guarded means the model can't insert to that particular column.
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);
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...
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.
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.
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
}
You can use $product->getChanges()
on Eloquent model even after persisting. Check docs here
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With