I want to increment the calls / views for a blog. In the controller I have the following two lines:
$post->views = $post->views 1;
$post->save();
I wonder if the update method might be faster?
Another question about incrementing. Is there a Laravel function that can be used to increment?
CodePudding user response:
In my opinion, both writing methods should be equally fast. Here is your second question. You can use increment()
function.
// 1.
Post::where('id', $id)->increment('views');
// 2.
Post::find($id)->increment('views');
// 3.
$post->views ;
$post->save();
CodePudding user response:
Both methods will be broadly the same, update()
also calls save()
.
public function update(array $attributes = [], array $options = []) { if (! $this->exists) { return false; } return $this->fill($attributes)->save($options); }