Relationship problems? (relationship with where)

Hello, I have this relationship at my model:
public function customer_price_change_beta()
{
return $this->hasOne(CustomerPriceChangesBeta::class, 'customer_id', 'customer_id')
->where('product_id', $this->product_id)
;
}
public function customer_price_change_beta()
{
return $this->hasOne(CustomerPriceChangesBeta::class, 'customer_id', 'customer_id')
->where('product_id', $this->product_id)
;
}
The relationships work very well outside filamentphp (eg at controller and lavarel's blade) At filamentphp I get an empty string. If I remove the where statement the relationship returns data (but it is wrong) This is a Filament bug?
7 Replies
Povilas K
Povilas K16mo ago
I would install Laravel debugbar and see what SQL queries are executed, then you would trace the reason of that empty string
JimKarvo
JimKarvoOP16mo ago
@povilas_korop thank you for your reply. Seems like the filament has its own logic for relationships. This is what filament tryies to get from SQL
select * from customer_price_changes_beta where product_id is null and customer_price_changes_beta.customer_id in (12, 13, 23, 25)
select * from customer_price_changes_beta where product_id is null and customer_price_changes_beta.customer_id in (12, 13, 23, 25)
The two tables are the same (one is a copy of the other) and I just want to test the new logic of auto changing the product price.
JimKarvo
JimKarvoOP16mo ago
I have dumped the
$this->product_id
$this->product_id
, at relationship, and seems that it is getting two empty product_id's
Shinra
Shinra16mo ago
hello, i also have the same requirement for where conditions on relationship this is my workaround, note the modifyQueryUsing
Forms\Components\Select::make('defect')
->label(__('Defect'))
->native(false)
->relationship(name:'defect_relationship',
titleAttribute: 'options_desc',
modifyQueryUsing: fn (Builder $query, string $context) => $query->defectOnly()->activeOnly($context == 'create'))
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->options_value} : {$record->options_desc}")
->searchable(['options_value', 'options_desc'])
->forceSearchCaseInsensitive()
Forms\Components\Select::make('defect')
->label(__('Defect'))
->native(false)
->relationship(name:'defect_relationship',
titleAttribute: 'options_desc',
modifyQueryUsing: fn (Builder $query, string $context) => $query->defectOnly()->activeOnly($context == 'create'))
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->options_value} : {$record->options_desc}")
->searchable(['options_value', 'options_desc'])
->forceSearchCaseInsensitive()
JimKarvo
JimKarvoOP16mo ago
@shinra5252 so it looks like a filamentphp bug?
Shinra
Shinra16mo ago
idk if its bug or intended, need an insight from the dev
cheesegrits
cheesegrits16mo ago
What exactly are you trying to do with that relationship? Show the code where you reference it in your resource.
Want results from more Discord servers?
Add your server