The Silent Filler: Why $model->save() Appears to Work But Doesn't Persist Changes
It is a familiar pattern in Laravel development: you assign new values to model properties, call save(), receive no exceptions, and get a boolean true back. Logically, the database should have updated. However, if you immediately fetch the record from the database using Post::find($id), the changes are missing. The data is untouched.
This specific scenario usually stems from how Laravel's Eloquent change detection works in conjunction with mass assignment protection. When you inspect the source code provided, the controller logic updates $post->title and $post->body. You might assume that because the properties have new values, the query builder will generate an UPDATE statement when save() is called. But there is a specific condition where this assumption fails: the model's internal tracking of changed attributes.
The Role of Mass Assignment
The root cause lies in the $fillable property defined on your Post model:
protected $fillable = [
'type',
'title',
'body',
'user_id',
];
While this array defines which attributes can be set via mass assignment (like $post->fill(['title' => 'New Title'])), it does not automatically mark those properties as "dirty" just because they hold new values in memory. Eloquent relies on the isDirty() method to determine if a specific column should be included in the SQL UPDATE statement.
However, there is a more subtle issue often overlooked when debugging this exact symptom. If your model definition includes $guarded = [] (which it doesn't explicitly here, but let's consider the fillable context) or if you are inadvertently resetting the model state, the save might succeed logically but update nothing physically.
In the provided controller snippet, the logic branches based on $post->type.
if ($post->type == 1) {
// ... validation and assignment
} else {
// ... validation and assignment
}
If the code path assigns new values to $post->title and $post->body, Eloquent should theoretically detect these changes. But why isn't it detecting them? The answer often lies in how the model is loaded versus how it is mutated.
Verification via isDirty()
The most direct way to diagnose this silence is to check whether Eloquent believes the model has changed attributes. Immediately before calling save(), add a debug check:
$post->title = request('title');
$post->body = request('body');
// Debug check
if ($post->isDirty(['title', 'body'])) {
echo "Attributes are dirty, save should occur.\n";
} else {
echo "No attributes are marked as changed.\n";
}
$post->save();
If this prints "No attributes are marked as changed," you have confirmed that Eloquent is not generating an UPDATE query. This usually happens if the model instance being saved is somehow identical to the one retrieved from the database regarding its original state, or if a previous operation reset the dirty flags.
A common pitfall occurs when developers use $post = Post::findOrFail($id); and then immediately run validation that might involve filling attributes via ->fill() or mass assignment without realizing it clears the dirty state before the manual assignment happens. However, in your specific code, you are assigning properties directly ($post->title = ...).
The touch and Timestamps Factor
Another frequent cause for this "silent failure" involves timestamps. If your Post model has a updated_at column (which is standard), Eloquent automatically updates it when the model is dirty. However, if you have custom logic or traits that rely on specific dirty states, conflicts can arise.
Consider the behavior of touch(). If you were to manually update timestamps, you might inadvertently trigger or suppress certain save behaviors depending on your configuration. But in this case, since no error is thrown and save() returns true, the database engine received a valid command. The issue is strictly about which columns are included in that command.
Ensuring Persistence
To guarantee persistence, ensure that you are not accidentally re-fetching the model from the database after modifying it but before saving. For example:
// DO NOT DO THIS unless you want to reset changes
$fetchedPost = Post::find($post->id);
$post = $fetchedPost;
If your code does this, any unsaved changes are lost because $post now points to a fresh copy from the database with old values. Your current snippet avoids this, which is good.
The definitive fix involves ensuring that the attributes you intend to update are indeed marked as dirty before save() is invoked. If manual assignment isn't triggering the state change you expect, verify that you aren't using any traits or observers that might be resetting the $attributes array prematurely.
Finally, remember that returning true from save() only indicates that the operation completed without throwing an exception. It does not explicitly confirm that rows were modified. To be absolutely certain in production code, developers often check the affected rows count returned by the query builder if they need strict guarantees, though for standard updates, verifying isDirty() is the standard debugging approach.
If you are facing persistent issues where save() returns true but data doesn't persist, reviewing your model's $guarded or $fillable arrays alongside any custom casts or accessors is often the key to unlocking the silent failure.