Laravel 5 isDirty() always returns false
$model->update()
updates and saves the model. Therefore, $model->isDirty()
equals false as the model has not been changed since the last executed query (which queries the database to save the model).
Try updating the model like this:
$partner = Partner::find($id);
foreach ($partnerData as $column => $value) {
if ($column === 'id') continue;
$partner->$column = $value;
}
if ($partner->isDirty()) {
// should be dirty now
}
$partner->save(); // $partner will be not-dirty from here
Laravel Eloquent update just if changes have been made
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
}
Laravel save() method returning true but not updating records
This line in the Opportunity model fixed the issue.
Protected $primaryKey = "opportunityID";
Although it is difficult to understand why it was still possible to retrieve the data and create a new record.
Laravel - Check if a value of a column was changed
There is a function isDirty for that in Laravel.
Eloquent provides the isDirty, isClean, and wasChanged methods to examine the internal state of your model and determine how its attributes have changed from when the model was originally retrieved.
The isDirty method determines if any of the model's attributes have been changed since the model was retrieved. You may pass a specific attribute name or an array of attributes to the isDirty method to determine if any of the attributes are "dirty". The isClean method will determine if an attribute has remained unchanged since the model was retrieved. This method also accepts an optional attribute argument:
use App\Models\User;
$user = User::create([
'first_name' => 'Taylor',
'last_name' => 'Otwell',
'title' => 'Developer',
]);
$user->title = 'Painter';
$user->isDirty(); // true
$user->isDirty('title'); // true
$user->isDirty('first_name'); // false
$user->isDirty(['first_name', 'title']); // true
$user->isClean(); // false
$user->isClean('title'); // false
$user->isClean('first_name'); // true
$user->isClean(['first_name', 'title']); // false
$user->save();
$user->isDirty(); // false
$user->isClean(); // true
The wasChanged method determines if any attributes were changed when the model was last saved within the current request cycle. If needed, you may pass an attribute name to see if a particular attribute was changed:
$user = User::create([
'first_name' => 'Taylor',
'last_name' => 'Otwell',
'title' => 'Developer',
]);
$user->title = 'Painter';
$user->save();
$user->wasChanged(); // true
$user->wasChanged('title'); // true
$user->wasChanged(['title', 'slug']); // true
$user->wasChanged('first_name'); // false
$user->wasChanged(['first_name', 'title']); // true
Similar question was posted here:
Laravel 5 isDirty() always returns false
Field data changed is not working in Laravel
It doesn't work that way. Model's isDirty()
will return true if the fields have been modified on the model itself. It expects one optional parameter, which is the list of parameters to check, not their new values. If you check isDirty()
right after Model::find()
, it will always return false.
So you need to do it the other way around:
- Find the model
- Set the new fields from the request to it (don't save!)
- Check
isDirty()
Related Topics
How to Dynamically Set Table Name in Eloquent Model
File Upload PHP $_Files Undefined Index Error
How to Keep Leading Zeros in PHP Integer
Google Calendar API Batch Request PHP
PHP Domdocument - Get HTML Source of Body
How to Get/Set Session_Id() or Should It Be Generated Automatically
PHP Get Height and Width in PDF File Proprieties
JSON_Decode() Returning Error "Notice: Trying to Get Property of Non-Object"
Phpunit: Doing Assertions on Non-Public Variables
Need to Merge Multiple PDF's into a Single PDF with Table of Contents Sections
PHP Static Method Call with Variable Class Name and Namespaces
Does the Use Keyword in PHP Closures Pass by Reference
Best Debug Tool to Debug Ajax Request in PHP
Are PHP Keywords Case-Sensitive
Key of Null Variable Equals Null Not Error
What Is the Significance of Application Key in a Laravel Application