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
You need to prefix references to the fields in
user
withuser.
, eg:You should create a separated component, and call it from the resources you need it in.
will generate your Infolist class on app/Infolists/Components.
There you can describe the schema with the build() method:
Then, in your Resource: