Error when trying to sort table by MorphTo relationship

I have a table with a model (Person) and that model has a MorphTo relationship type called user(). In generating the table, I'm trying to do the following (some fields removed for the example but they do not matter):
return $table
->columns([
TextColumn::make('user.name')
->label('Name')
->searchable()
->sortable(),
])
->defaultSort('name', 'asc')
return $table
->columns([
TextColumn::make('user.name')
->label('Name')
->searchable()
->sortable(),
])
->defaultSort('name', 'asc')
but when I try to view the table, I get this:
Illuminate \ Database \ QueryException
PHP 8.3.14
11.33.2
SQLSTATE[HY000]: General error: 1 no such column: laravel_reserved_0.
select * from "people" where "people"."deleted_at" is null order by (select "name" from "people" as "laravel_reserved_0" where "laravel_reserved_0"."" = "people"."user_id" and "laravel_reserved_0"."deleted_at" is null) asc limit 10 offset 0
Illuminate \ Database \ QueryException
PHP 8.3.14
11.33.2
SQLSTATE[HY000]: General error: 1 no such column: laravel_reserved_0.
select * from "people" where "people"."deleted_at" is null order by (select "name" from "people" as "laravel_reserved_0" where "laravel_reserved_0"."" = "people"."user_id" and "laravel_reserved_0"."deleted_at" is null) asc limit 10 offset 0
The where "laravel_reserved_0"."" looks suspicious to me (why is the column name blank?), and I suspect it has something to do with the MorphTo relationship, but I'm not clear where to even continue looking and troubleshooting. I have confirmed: - Without the defaultSort(), the table displays the correct user.name value so it's something to do with this call - Every Person has a valid user_id and user_type entry filled in - Every referenced user_id has a valid (non-null) name value I'm also having what I think might be a similar issue with the related form for this table; if I try to add live() to the email address field which lives on the same user relationship, it doesn't actually fire. I can see the network call to /update happening in the Dev Console, but there's no data in the components.0.effects.returns array which I assume is where any validation errors would otherwise appear? Is anyone able to give me a hand?
6 Replies
nexxai
nexxaiOP2w ago
More notes: - If I remove the defaultSort() on the name column but then try to sort the column manually by clicking on the chevron at the top of the column, the same error shows up - The same error also appears to affect another column on the same table which is also on the related model, strengthening my belief that it is somehow related to the MorphTo aspect of the relationship - Other columns which are not related are sortable just fine when the chevron is clicked Single bump
awcodes
awcodes2w ago
Yea. Laravel reserved is odd. What are the actual relationships you are defining on your models?
dissto
dissto2w ago
Just a guess though, your model is named Person but your tables are for People. Probably related to Laravels internal pluralization. Did you provide the necessary parameters for the relationships since you deviate from the regular naming conventions? 🤔
awcodes
awcodes2w ago
I’m thinking along the same lines.
nexxai
nexxaiOP2w ago
Person model:
public function user(): MorphTo
{
return $this->morphTo();
}
public function user(): MorphTo
{
return $this->morphTo();
}
User model:
public function person(): MorphOne
{
return $this->morphOne(Person::class, 'user');
}
public function person(): MorphOne
{
return $this->morphOne(Person::class, 'user');
}
I've also tried with a straight hasOne relationship on the User model, and the same thing happens. Also of note, the table is being displayed on the PersonResource in Filament, so I'm not sure how I would fix the pluralization; it's not going user->person/people, it's going person->user When you say that I "deviate from the regular naming conventions", can you explain what you mean? Like I think you mean person vs. people, but I was always under the impression that the model should be titled the singular of the noun, and the table gets named as the plural. Did I do or assume something wrong here? @awcodes I'm only tagging you here since you're a core maintainer of Filament and this actually seems like something that may be an issue with Filament, specifically with regards to whereHas calls. As per https://github.com/laravel/framework/pull/28928 (and linked from https://github.com/laravel/framework/issues/18523 which describes a similar - but technically not identical - issue) whereHas doesn't work on MorphTo relationships, and my assumption is that in the building of the table, it's doing a whereHas() call instead of the whereHasMorph() which was submitted in the linked PR.
I did a search through the entire filamentphp GitHub organization for the term "wherehasmorph" (https://github.com/search?q=org%3Afilamentphp+wherehasmorph&type=code) and got 0 results so it at least seems possible that Filament not checking for a morphable relationship and implementing the corresponding whereHas-type lookup could be the source of at least one issue. Do you have any suggestions about what I should do next? Is this something the team would even consider looking into? I feel like I'm not the first one who would ever be trying to do something like this, but my searching isn't turning up anything that could even lead me in a direction.
toeknee
toeknee2w ago
If you believe it to be a bug, please create a reproduction repository and submit a bug on Github. Tagging is agains our #✅┊rules .
Want results from more Discord servers?
Add your server