Why not just use updated_at?
Auto update of date field in laravel
Hi.,
Is it possible to auto update the column of date datatype in the database, as like as timestamps, when set true, whenever the data is updated or inserted.
How to do that?
The controller code
public $timestamps=true;
@sekar_r24 you can try this
controller store and update
public function store(Request $request)
{
$date = date_default_timezone_get();
YourModel::create([
'time' => $date,
]);
}
Did you find a solution?
i am getting the following error The GET method is not supported for this route. Supported methods: POST.
My db is
Schema::create('orders', function (Blueprint $table){
$table->date('date_inserted')->nullable(); }
and the controller is $sbtd = new sbtd; $sbtd->date_inserted
Ok that sounds like a totally different issue? Anyways. Post the url you are calling and your routes
@sinnbeck my route is:
Route::post('main/update','submitted@update')->name('update'); i am calling it in the localhost:8000/main/update
Show the code got calling it. A form I assume?
<form action="update" method="post">
//with different types of fields goes here..
<button type="submit" class="btn btn-sucess">SUBMIT</button>
</form>
It needs the url in action
<form action="/main/update" method="post">
@sinnbeck i am getting this error
"SQLSTATE[22007]: Invalid datetime format: 7 ERROR: invalid input syntax for type date: "UTC" " My column data type is set as date for your information.
@sekar_r24 The problem is due to MySQL server's time_zone setting being set to SYSTEM. Laravel is providing timestamps that were already converted to UTC, but the database is interpreting them as anther timezone ie(YOur server location) due to the time_zone setting. The times are actually being converted again internally by MySQL to a real UTC Unix timestamp representation, even though they appear to be UTC already in every query (I know right).
Because of this, at 20:00:39 (8 PM) local time your"UTC" timestamps are 02:00:39. MySQL interprets these times as something like US Central time, and because the time is between 02:00 and 03:00 (which is when clocks skip forward for US Central if your server is in the US), the time is invalid.
The best solution for a Laravel application is to force every database connection to use a +00:00 timezone (or whatever you have set as the application timezone in config/app.php) so there will not be a secondary conversion happening. This can be done in config/database.php:
'mysql' => [
// ...
'timezone' => '+00:00'
],
This way you are not at the mercy of your database server if it has a configured timezone that is different from your Laravel application. The other option is to change the database's time_zone setting, but then you still risk the bug recurring if you ever change hosts or need to rebuild the server for any reason (and don't configure the timezone correctly again).
@sekar_r24 can you please share your config/database.php: code, model, and controller
and change in your controller to
date_default_timezone_set('UTC');
hi.,
the config/database.php
<?php
use Illuminate\Support\Str;
return [
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'testt'),
'username' => env('DB_USERNAME', 'postgres'),
'password' => env('DB_PASSWORD', '*****'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
],
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer body of commands than a typical key-value system
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
],
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],
],
];
Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class sbtd extends Model
{
//
protected $table ='orders';
//public $timestamps=true;
//public $date=true;
}
Controller
function update(Request $req)
{
$date =date_default_timezone_set('UTC');
$sbtd->date_inserted=$date;
}
Hi Shows the following error
SQLSTATE[22007]: Invalid datetime format: 7 ERROR: invalid input syntax for type date: "1"
I am using postgres
Please or to participate in this conversation.