You may need to increment a field in a record of a table (a single attribute of a Laravel Model) and do not want update the updated_at timestamps.
Suppose the field is
popularity when you do this:
popularity, this will update the timestamps of that record too!
While you don’t want it.
$plan=Plan::find($planId);$plan->timestamps = false;
As an example see how laravel does the same when update the remember_me token at each logout without touching the timestamps.
In class EloquentUserProvider in Illuminate\Auth directory (this link) in the updateRememberToken() method Laravel first assigns the value (true or false) of timestamps to a temporary variable and after saving the remember_token in the $user model retrieves its original value.
Using a simple query builder which does not update timestamps on its own:
timestamps (updated_at) is not touched:
timestamps (updated_at) is touched:
->increment(‘popularity’,1,[‘updated_at’ => Carbon::now()]);
By default, Eloquent expects
updated_at columns to exist on your tables. If you do not wish to have these columns automatically managed by Eloquent, set the
$timestamps property on your model to
class Plan extends Model
* Indicates if the model should be timestamped.
* @var bool
public $timestamps = false;
In this case, whenever you want the timestamps to be updated, you need to manually assign them value like
$plan->updated_at = Carbon::now();
Thank you for reading! If you enjoyed this article:
Clap it ! Share it! Follow Me in Medium!
Panjeh — Medium
Read writing from Panjeh on Medium. Physicists PhD, Self-taught web developer #Learning, #laravelphp #Vuejs. Every day…
Also I’d like to hear your opinion on this article. If you have any doubt, question or suggestion please leave a comment below.
Have a very wonderful day!
Previous Stories You will Love:
Laravel call named routes in console internally php artisan command
How to call Laravel routes internally php artisan command
Telegram Bot Packages libraries Examples in Laravel, PHP, Node.js, Python, Java, Ruby, Swift, C++
If you want to learn more about Telegram bots, This Introduction to Bots would be a good guide to start.
Laravel Auth::routes() Email verification Reset password Authentication Registration routes
Auth::routes() is a helper class that helps you generate all the routes required for user authentication
Laravel validation username no space allowed alpha_dash or regex custom rule
If you want to restrict the username in Laravel to only accept letters, numbers, dashes and underscores and not space…
Laravel delete queued jobs using redis horizon artisan command
The question is how to delete all jobs which are queued in Laravel when you are using redis as the Queue driver.
Laravel detector mobile browser name version platform device robot crawler user language
Laravel define global constants Config php file
If you look at the main Laravel directory you can find these subdirectories:
Laravel session flash message include html tag link href
In Laravel you may insert a or strong tags into the flash message deliver to the session.
Laravel 502 Bad Gateway when starting Laravel Valet — Simple solution
The first solution is here: