skip to Main Content

I’m working on a Laravel project using Filament. I have two models: User and Car. Each Car belongs to a specific User. I want to display user information on the Car page by using the infoList defined in the UserResource class.

Here are the details of my models and resources:
User Model:

class User extends Model {
    public function cars() {
        return $this->hasMany(Car::class);
    }
}

Car Model:

class Car extends Model {
    public function user() {
        return $this->belongsTo(User::class);
    }
}

UserResource:

class UserResource extends Resource {
    public static function infoList(Infolist $infolist): Infolist {
        return $infolist
            ->schema([
                TextEntry::make('title'),
                TextEntry::make('slug'),
                TextEntry::make('content'),
            ]);
    }
}

CarResource:

class CarResource extends Resource {
    public static function infoList(Infolist $infolist): Infolist {
        return $infolist
            ->schema([
                TextEntry::make('model'),
                // How to include UserResource::infoList here?
            ]);
    }
}

I attempted to reuse the UserResource::infoList method in the CarResource to display user information. However, when I try to do this, Filament seems to look for fields like title in the Car model, but they should be read from $car->user (e.g., TextEntry::make(‘title’) should show $car->user->title).

Additionally, if I need to change the UserResource to accommodate this, it must be done in a way that allows it to be used not only in UserResource, but also in other resources like CarResource, HouseResource, and so on, at the same time.
Additional Information:

Laravel Version: 11.x
Filament Version: 3.x

Any guidance or examples would be greatly appreciated. Thanks!

2

Answers


  1. You need to prefix references to the fields in user with user., eg:

    TextEntry::make('user.title')
    
    Login or Signup to reply.
  2. You should create a separated component, and call it from the resources you need it in.

    php artisan make:infolist-layout
    

    will generate your Infolist class on app/Infolists/Components.

    There you can describe the schema with the build() method:

    public static function build(): array
    {
        return [ ... ];
    }
    

    Then, in your Resource:

    public static function infolist(Infolist $infolist): Infolist
    {
        return $infolist
            ->schema(UserInfolist::build())
            ->columns(1)
            ->inlineLabel()
            -> ...;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search