laravel icon indicating copy to clipboard operation
laravel copied to clipboard

Time field as Date field

Open IManAsap opened this issue 4 years ago • 3 comments

Hi guys, I have an error with my generated models, when my table has a field in type "Time" it is understanding as type "Date" and when I go to run the model it returns me the following error:

"Unexpected data found. Unexpected data found. Data missing"

IManAsap avatar May 22 '20 14:05 IManAsap

I think you might be able to fix it by overriding the $casts attribute (if you've enabled base_files). Pull requests are always welcomed and encouraged to fix bugs.

CristianLlanos avatar May 23 '20 20:05 CristianLlanos

Hi,

with

'casts' => [ 'json' => 'json', 'heure' => 'time'

I get

protected $casts = [ 'heure_debut_matin' => 'time',

but I still have

protected $dates = [ 'heure_debut_matin',

and "Unexpected data found. Unexpected data found. Data missing"

IManAsap avatar May 26 '20 07:05 IManAsap

If you enable "base files", you'll get two files looking something similar to:

// One being the base model which will be replaced with updates every time you run `php artisan code:models`
// app/Models/Base/Movie.php
class Movie {
    protected $casts = [
        'showtime' => 'time',
        // ...
    ];

    protected $dates = [
        'showtime',
        // ...
    ];
}


// And another one which you can modify at will and won't be overridden by `php artisan code:models`
// Whenever you need a Movie model, you should use this one instead of the base model
// app/Models/Movie.php
class Movie extends Base\Movie {
    // So, if you need to, you can just override the $dates property to look like this and get the desired effect
    protected $dates = [];
}

What I'm suggesting is a workaround, though. The parser should be updated, so that this behaviour would be handled gracefully by default. However, a PR is needed.

CristianLlanos avatar May 31 '20 05:05 CristianLlanos