Table column reference
Hi all, I have a table in which I want to display Restaurant Name and User Name rather than restaurant_id or user _id.
the code I have for the columns in the resource file is:
->columns([
Tables\Columns\TextColumn::make('body')->sortable(),
Tables\Columns\TextColumn::make('report_date')
->dateTime('M j,Y')->sortable(),
Tables\Columns\TextColumn::make('restaurant_id')->label('Restaurant'),
Tables\Columns\TextColumn::make('user_id')->label('Name')->sortable(),
if I change the code to
Tables\Columns\TextColumn::make('restaurant.name')->label('Restaurant'),
I get an empty field on the table.
I have a DB tables for users, restaurants and one restaurant_user table with foreign keys for restaurant_id and user_id.
The definition of the restauran_user table is :
public function up(): void
{
Schema::create('restaurant_user', function (Blueprint $table) {
$table->foreignId('restaurant_id')->constrained()->cascadeOnDelete;
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamps();
});
}
I also defined the relationships in both model files for users and restaurants. the relationships are :
for the user model:
public function restaurant(): BelongsToMany
{
return $this->belongsToMany(Restaurant::class);
}
for the restaurant model:
public function users(){
return $this->belongsToMany(User::class);
}
I appreciate any guidance on making appear in the table the name of the restaurant and user rather than the Ids
Thanks
6 Replies
in the filament resource:
Filament
Column relationships - Columns - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
unfortunately, it doesn't work for me
it shows an empty field
So therefore your relationship is not working
You have set a belongsToMany which is why it is empty
Change tit o a belongsTo()
thanks toeknee