Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date field on a related model returns string and fails with DateTimeInterface check when displayed as related resource #57

Closed
anderly opened this issue Aug 23, 2018 · 1 comment

Comments

@anderly
Copy link

anderly commented Aug 23, 2018

Laravel Nova 1.0.5 with fresh install of latest Laravel 5.6.

A Nova Date field dob on a User resource works properly when viewing/editing the User resource directly, but not when displaying that resource as a related resource through a parent resource (Company).

Screenshot of front-end error showing the related resource: http://ander.ly/WNsUuZ

Screenshot of the network call that's failing: http://ander.ly/WTye9w

Again, this only happens when viewing the Company resource when it tries to show the related User resource.

Here's the setup:

Given the following db tables:

companies table

column datatype length
id int 10
name varchar 255

users table

column datatype length
id int 10
name varchar 255
email varchar 255
password varchar 255
dob date
company_id int 10
created_at timestamp
udpated_at timestamp

Eloquent Models
App\Company.php

class Company extends Model
{
    /**
     * Get the users for this company
     */
    public function users()
    {
        return $this->hasMany(User::class, 'company_id', 'id');
    }
}

App\User.php

class User extends Authenticatable
{
    use Notifiable;
    
    /* Removed for brevity... */
    /**
     * Get company for this user
     */
    public function company()
    {
        return $this->belongsTo(Company::class);
    }
}

Nova Resources
App\Nova\Company.php

class Company extends Resource
{
    /* Removed for brevity... */
    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            Text::make('Name')
                ->sortable()
                ->rules('required', 'max:255'),

            HasMany::make('Users'),
        ];
    }
}

App\Nova\User.php

class User extends Resource
{
    /* Removed for brevity... */
    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('Name')
                ->sortable()
                ->rules('required', 'max:255'),
            Date::make('dob')
                ->sortable(),
            BelongsTo::make('Company'),
        ];
    }
}
@anderly anderly closed this as completed Aug 23, 2018
@anderly
Copy link
Author

anderly commented Aug 23, 2018

Tracked this down to an inheritance issue on the model and the $dates were not set on the root model, which caused the invalid cast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant