F
Filament17mo ago
RT2023

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
toeknee
toeknee17mo ago
in the filament resource:
TextColumn::make('restaurant.name')->sortable(),
TextColumn::make('restaurant.name')->sortable(),
RT2023
RT2023OP17mo ago
unfortunately, it doesn't work for me
RT2023
RT2023OP17mo ago
it shows an empty field
toeknee
toeknee17mo ago
So therefore your relationship is not working You have set a belongsToMany which is why it is empty Change tit o a belongsTo()
RT2023
RT2023OP17mo ago
thanks toeknee
Want results from more Discord servers?
Add your server