FilamentF
Filament15mo ago
MatXps

search relation from resource in table throw error as name mismatch

I have 2 models

#Model Item
public function favorites()
{
return $this->hasMany('App\Models\Favorite');
}

#Model Favorite
protected $table = 'item_user';
public function item()
{
return $this->belongsTo('App\Item');
}

Database
Table item
id
user_id
otherfields...

Table item_user
id
user_id
item_id

I created a Filament 'Favorite Resource'
public static function table(Table $table): Table
{
return $table
->query(function (Item $query) {
return $query->whereHas('favorites', function($query){
$query->where('user_id', '=', auth()->user()->id);
});
})
->recordUrl(
fn (Item $record): string => "/listing/".$record->item_slug,
shouldOpenInNewTab: true,
)
->columns([
Stack::make([
// Columns
Tables\Columns\TextColumn::make('item_title')
->searchable(),
Tables\Columns\TextColumn::make('item_price')
->money('CAD'),
Tables\Columns\ImageColumn::make('attachments')
->label('Image')
->limit(2)
->circular()
->stacked()
->limitedRemainingText()
->defaultImageUrl(url('/images/placeholder.png')),
Tables\Columns\TextColumn::make('favorite.user_id')
->searchable(),
//
]),

])
...


The error I get is when I try to search the table


Illuminate \ Database \ QueryException

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'favorite' in 'where clause'
Was this page helpful?