Search code examples
databaselaravellaravel-5.3strtotimedate-conversion

how to convert a date into an integer in laravel


what I have learnt from the documentation is,I need to make an mutator function which should be getting that value and than converting it into a table.But I am constantly getting the error of SQLSTATE[01000]: Warning: 1265 Data truncated for column 'valid_to' at row 1 (SQL: insert into promotions (name, city_id, valid_to, is_active, type, limit, updated_at, created_at) values (a, Jadah, 2018-02-28, 1, 1, a, 1519818982, 1519818982))

Model part of the code:

public function setValidFromValueAttribute($date)
    {
        $this->attributes['valid_from'] = strtotime($date);
    }
    public function setValidToValueAttribute($date)
    {
        $this->attributes['valid_to'] = intval($date);
    }

Blade part of the code for date is

                <div class="form-group">
                    {!! Form::label('valid_from','Valid From') !!}
                    {!! Form::date('valid_from',\Carbon\Carbon::now()) !!}
                </div>
                <div class="form-group">
                    {!! Form::label('valid_to','Valid Till') !!}
                    {!! Form::date('valid_to',\Carbon\Carbon::now()) !!}
                </div>

migration part of the code

$table->increments('id');
            $table->string('name')->unique();
            $table->integer('valid_from')->default(1);
            $table->integer('valid_to')->default(1);
            $table->tinyInteger('is_active');
            $table->tinyInteger('type');
            $table->string('city_id')->default('Jadah');
            $table->string('limit')->unsigined();
            $table->integer('updated_at')->unsigined();
            $table->integer('created_at')->unsigined();

any help would be highly appreciated.


Solution

  • All i needed to do was to make

    protected $dates = [
            'valid_from',
            'valid_to'
        ];
    

    and remain the valid_from and valid_to into the migration as

    $table->integer('valid_from');
    $table->integer('valid_to');
    

    and it all worked fine