Laravel migration. Problem with timestamp

1

I have a migration for a travel table. The code is as follows:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateToursTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tours', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('operator_id')->unsigned();
            $table->string('destino', 100);
            $table->timestamp('inicio');
            $table->timestamp('final');
            $table->integer('duracion')->unsigned();
            $table->text('detalles');
            $table->timestamps();
            /* El campo deleted_at */
            $table->softDeletes();
            /** La clave foránea para relacionar esta tabla con operators. */
            $table->foreign('operator_id')->references('id')->on('operators');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tours');
    }
}

The problem appears with the final field. I want to declare it as timestamp . I do it with the field inicio and it does not give problems, but with the field final it gives me an error:

   Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'final' (SQL: create table 'tours' ('id' int unsigned not null auto_increment primary key, 'operator_id' int unsigned not null, 'destino' varchar(100) not null, 'inicio' timestamp not null, 'final' timestamp not null, 'duracion' int unsigned not null, 'detalles' text not null, 'created_at'
timestamp null, 'updated_at' timestamp null, 'deleted_at' timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

  at C:\xampp\htdocs\viajes\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'final'")
      C:\xampp\htdocs\viajes\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      C:\xampp\htdocs\viajes\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

The fact is that I take the query that shows the error, which is the following:

create table 'tours' ('id' int unsigned not null auto_increment primary key, 'operator_id' int unsigned not null, 'destino' varchar(100) not null, 'inicio' timestamp not null, 'final' timestamp not null, 'duracion' int unsigned not null, 'detalles' text not null, 'created_at'
timestamp null, 'updated_at' timestamp null, 'deleted_at' timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'

I run it directly on PHPMyAdmin and it works without problems.

If the field final I declare as date , instead of timestamp , the migration runs without problems, but, of course, I do not have the type of data I want.

In the Tour.php model there seems to be nothing out of place:

<?php

namespace App;

use App\Operator;
use App\Customer;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Tour extends Model
{
    use SoftDeletes;
    protected $table = 'tours';
    protected $fillable = [
        'operator_id',
        'destino',
        'inicio',
        'duracion',
        'final',
        'detalles'
    ];
    protected $dates = [
        'deleted_at'
    ];

    public function operator()
    {
        return $this->belongsTo(Operator::class);
    }

    public function customers()
    {
        return $this->belongsToMany(Customer::class);
    }
}

Does anyone know what this may be? Thanks to everyone.

    
asked by Chefito 03.12.2018 в 00:20
source

1 answer

1

You can declare a default value for the timestamp() field by using the useCurrent() method so that your migration looks like this:

            $table->timestamp('inicio')->useCurrent();
            $table->timestamp('final')->useCurrent();

When returning from the database a stored value with these two columns; you should be getting something similar to this

#original: array:6 [▼
    "id" => 1
    "content" => "hello world"
    "inicio" => "2018-12-03 00:39:12"
    "fin" => "2018-12-03 00:39:12"
    "created_at" => "2018-12-03 00:39:12"
    "updated_at" => "2018-12-03 00:39:12"
  ]
    
answered by 03.12.2018 / 01:43
source