When updating my Post
model, I run:
$post->title = request('title');
$post->body = request('body');
$post->save();
This does not update my post. But it should according to the Laravel docs on updating Eloquent models. Why is my model not being updated?
- I get no errors.
- The post does not get updated in the db.
- Besides not being updated in the db, nothing else seems odd. No errors. Behavior as normal.
- Result of running this test to see if
save
succeeded wastrue
. - This Laravel thread was no help
Post
model:
class Post extends Model
{
protected $fillable = [
'type',
'title',
'body',
'user_id',
];
....
}
Post
controller:
public function store($id)
{
$post = Post::findOrFail($id);
// Request validation
if ($post->type == 1) {
// Post type has title
$this->validate(request(), [
'title' => 'required|min:15',
'body' => 'required|min:19',
]);
$post->title = request('title');
$post->body = request('body');
} else {
$this->validate(request(), [
'body' => 'required|min:19',
]);
$post->body = request('body');
}
$post->save();
return redirect('/');
}
Bonus info
Running dd($post->save())
returns true
.
Running
$post->save();
$fetchedPost = Post::find($post->id);
dd($fetchedPost);
shows me that $fetchedPost
is the same post as before without the updated data.
dd($post->save())
? – Helpdd($post->save())
returnstrue
. I added this in the question. – Virgulatedd($post->isDirty())
return? – Chaetafalse
...... I followed this SO question to ensure I was doing it right. – Virgulaterequest
should be$request
? – Kristelkristenrequest('title')
and/orrequest('body')
must be identical? – Kristelkristendd(request('title'))
: This is my title and now it has been updated. – Virgulatetitle
however it is arequired
field. – Zymogentitle
is not required – Virgulate