F
Filament16mo ago
Adam

Table Relationship 'name' not 'id'

Apologies if this has been asked before but I can seem to find the solution in the docs or in Discord. Currently, when viewing records using the table builder, I can see a column's relationship's id but I want it to display the relationship's name column. The model's relationship:
public function resort() {
return $this->belongsTo(Resort::class, 'id', 'resort');
}
public function resort() {
return $this->belongsTo(Resort::class, 'id', 'resort');
}
Here is what I've tried: TextColumn::make('resort.resort'), this results in SQLSTATE[42S22]: Column not found: 1054 Unknown column 'resorts.resort' in 'where clause' I've also tried: TextColumn::make('resorts.resort'), and the table column is blank this just outputs the id TextColumn::make('resort'),
9 Replies
LeandroFerreira
LeandroFerreira16mo ago
$this->belongsTo(Resort::class, 'resort', 'id');
$this->belongsTo(Resort::class, 'resort', 'id');
Adam
AdamOP16mo ago
The relationship model is fine, I can get the names of the relationship in the Editor, but only their id shows up in the main list view. Here's an example: - First screen shot is the list (table) view. - Second screen shot is the edit view.
ejoi8
ejoi816mo ago
try
TextColumn::make('resort.name')
TextColumn::make('resort.name')
resort is the relation & the name is the column name
Adam
AdamOP16mo ago
Tried that prior, and this is what it results in:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'resorts.resort' in 'where clause'
SELECT * FROM `resorts` WHERE `resorts`.`resort` IN (1, 2)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'resorts.resort' in 'where clause'
SELECT * FROM `resorts` WHERE `resorts`.`resort` IN (1, 2)
Code for reference:
return $table
->columns([
TextColumn::make('name'),
TextColumn::make('resort.name')->label('Resort'),
return $table
->columns([
TextColumn::make('name'),
TextColumn::make('resort.name')->label('Resort'),
If it helps, here is the migration: $table->foreignId('resort')->nullable()->constrained()->references('id')->on('resorts');
ejoi8
ejoi816mo ago
If possible, try to rename the foreignId to resort_id and see how it goes. So the table column will be resort_id
Adam
AdamOP16mo ago
No luck still. The id of the relationship is still being output.
snowden98
snowden9816mo ago
I got the same problem. In that the relationships are fine but to display a name from the column relationship is a problem. Does not display anything.
dissto
dissto16mo ago
If you reference .name do you get that there is no relationship error or no error at all? please show the entire model (if you can) and the entire component
snowden98
snowden9815mo ago
Did you find a solution
Want results from more Discord servers?
Add your server