How to add `created_at` column with `sortable()` to `BelongsToMany` RelationManager correctly
I have a RelationManager with a
BelongsToMany
relationship, and both the pivot table and the relationship table has a created_at
column. I try to add the column to the table like this:
This shows a date column in my table as expected, but when I try to sort the table by the created_at
column, I get this Ambiguous column
error:
How do I make sure that the table uses the created_at
column from the users
table when sorting?Solution:Jump to solution
You could try:
```php
->sortable(query: function (Builder $query, string $direction): Builder {
return $query...
12 Replies
What is your relationship... what if you take off sortable() ?
If I take off
sortable()
, then nothing changes with the column data, but the sortable functionality (clickable column header) is removed. The shown created_at
data is from my Users
model.
This RelationManager is on my GroupResource
:
And the relevant setup and table of the UsersRelationManager
looks like this:
bumpby setting it to be created_at and sortable() should be fine
Can you show your relationship for the users?
Do you mean the users relationship in my Group model?
It looks like this:
setting it to be created_at and sortable() gives me the error I shared when I try to sort the table by created_at
Do you understand whats happening? why the SQL error occurs? it's because there are multiple created_at basically, so part of it is setting is as created_at. But if the relationship is users it should be scoped to the relation. Unless you have a custom filter/relationship shorting by created_at. So try and find where the created_at is defined
@toeknee What I understand is that there is a
created_at
column in the users
table and in the group_user
pivot table, which is what is causing this ambiguous column error, as the sortable()
method does not know whether to use the scoped users
table or the group_user
table. Am I wrong?
The created_at
on the users table is defined in the migration file with $table->timestamps();
And on the relationship in the Group
model, the created_at
column is defined by the ->withTimestamps()
on the group_user
pivot table.I suspect the problem is you have timestamps on the group_user privot as it shouldn't really matter when the pivot was added persay so when the pivot merges with the users it's not splitting the created_at which creates the ambiguious
Hmm.. But I want to keep track of when a user became a member of a group. Is there no way to explicitly choose the created_at from the users table?
Solution
You could try:
Thank you @toeknee, that works like a charm! Is this something that could be considered a bug in the sortable method or the RelationManager? Or is it just how laravel works? I could imagine that this issue could occur for others in the future.
Welcomes! It could be possibly... I'm not familiar with the inner workings too deep. It could be theroised that we should scope the query always with the model which would advoid the issue. But there could be other reasons. having created_at and updated_at is quite rare on a pivot tbh
Alright, well at least this thread would be here in case others experience the same issue.