Tengo un proyecto laravel configurado en localhost donde creé un comando en el programador que es el siguiente:
DB::table('relic')->where('created_at', '<', 'NOW() - INTERVAL 1 MONTH')->delete();
Probé ejecutando el comando del planificador a través de craft y obtengo este resultado:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1525 Incorrect TIMESTAMP value: 'NOW() - INTERVAL 1 MONTH' (SQL: delete from `relic` where `created_at` < NOW() - INTERVAL 1 MONTH)
Probé los comandos
SELECT * FROM relic WHERE created_at < CURRENT_TIMESTAMP() - INTERVAL 1 MONTH;
y
SELECT * FROM relic WHERE created_at < NOW() - INTERVAL 1 MONTH;
en mysql y ambos funcionaron correctamente.
¿Qué me estoy perdiendo?
La biblioteca Carbon se incluye con Laravel, y es la forma "estándar" de manejar fechas y horas. Puedes escribir tu consulta de la siguiente manera:
DB::table('relic') ->where('created_at', '<', \Carbon\Carbon::now()->subMonth()) ->delete();Si desea utilizar su sintaxis, puede hacerlo con una instrucción whereRaw como esta:
DB::table('relic') ->whereRaw('created_at < NOW() - INTERVAL 1 MONTH') ->delete();