Laravel 4+
Note for Laravel 5 users: You'll need to call DB::enableQueryLog()
before executing the query. Either just above the line that runs the query or inside a middleware.
In Laravel 4 and later, you have to call DB::getQueryLog()
to get all ran queries.
$queries = DB::getQueryLog();
$last_query = end($queries);
Or you can download a profiler package. I'd recommend barryvdh/laravel-debugbar, which is pretty neat. You can read for instructions on how to install in their repository.
Laravel 3
In Laravel 3, you can get the last executed query from an Eloquent
model calling the static method last_query
on the DB
class.
DB::last_query();
This, however, requires that you enable the profiler
option in application/config/database.php
. Alternatively you could, as @dualed mentioned, enable the profiler
option, in application/config/application.php
or call DB::profile()
to get all queries ran in the current request and their execution time.
Given it's a raw expression, you should use DB::raw()
to set CURRENT_TIMESTAMP
as a default value for a column:
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
This works flawlessly on every database driver.
As of Laravel 5.1.25 (see PR 10962 and commit 15c487fe) you can now use the new useCurrent()
column modifier method to achieve the same default value for a column:
$table->timestamp('created_at')->useCurrent();
Back to the question, on MySQL you could also use the ON UPDATE
clause through DB::raw()
:
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
Again, as of Laravel 8.36.0 (see PR 36817) you can now use the new useCurrentOnUpdate()
column modifier method together with the useCurrent()
modifier to achieve the same default value for a column:
$table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();
Gotchas
MySQL
Starting with MySQL 5.7, 0000-00-00 00:00:00
is no longer considered a valid date. As documented at the Laravel 5.2 upgrade guide, all timestamp columns should receive a valid default value when you insert records into your database. You may use the useCurrent()
column modifier (from Laravel 5.1.25 and above) in your migrations to default the timestamp columns to the current timestamps, or you may make the timestamps nullable()
to allow null values.
PostgreSQL & Laravel 4.x
In Laravel 4.x versions, the PostgreSQL driver was using the default database precision to store timestamp values. When using the CURRENT_TIMESTAMP
function on a column with a default precision, PostgreSQL generates a timestamp with the higher precision available, thus generating a timestamp with a fractional second part - see this SQL fiddle.
This will led Carbon to fail parsing a timestamp since it won't be expecting microseconds being stored. To avoid this unexpected behavior breaking your application you have to explicitly give a zero precision to the CURRENT_TIMESTAMP
function as below:
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP(0)'));
Since Laravel 5.0, timestamp()
columns has been changed to use a default precision of zero which avoids this.
Thanks to @andrewhl for pointing out the Laravel 4.x issue in the comments.
Thanks to @ChanakaKarunarathne for bringing out the new useCurrentOnUpdate()
shortcut in the comments.
Best Answer
I resolved this case by using
errorlog
logging model and configuring webserver.1. Configure Laravel:
In
config/app.php
configuration file:Read more about Laravel log configuration: http://laravel.com/docs/5.1/errors#configuration
2. Configure webserver (in my case Nginx):