Calculate difference between two dates with timestamps in Laravel
问题 I have in my table a timestamps with created_at and updated_at $table->timestamps(); But I want to add another column that calculates the difference between created_at and updated_at in days Should I do that in SQL or in my controller? 回答1: You can do that from within your Eloquent model. Let's assume your model has the name User . You can create a computed field by defining an Accessor. class User extends Model { $dates = [ 'updated_at', 'created_at' ]; $appends = ['diffInDays']; public