Closed yzxh24 closed 2 months ago
Same kind of question but on a polymorphic relationship.
public function ratings()
{
return $this->morphMany(Rating::class, 'rateable');
}
TextColumn::make('ratings')
->label(__('Votes'))
->sortable();
@yzxh24 Hi! Try to remove pivot.
prefix here
And after that, it works well for me.
https://github.com/filamentphp/filament/assets/4639175/095e8be8-3486-44fd-b94d-f61db96bd7c2
@yzxh24 Hi! Try to remove
pivot.
prefix hereAnd after that, it works well for me.
Screencast.2024-04-22.10.55.40.mp4
@dmitry-udod Hi,my friend, thanks for your reply.
It is indeed correct to change it the way you said.
However, there is something wrong with my previous code, the questions
table also has a score
column for the question scores.
I have uploaded the code to the library https://github.com/yzxh24/filament-issue
After I added this column the sum
came out correctly, but the score
shown on each row of the table
is for the questions
table.
The final writeup should be something like this:
Tables\Columns\TextColumn::make('pivot.score')
->summarize(Tables\Columns\Summarizers\Sum::make())
/// Here we specify the score
->using(fn (Builder $query): string => $query->sum('score'))
)
->label('Score')
After writing it this way it passes on sqlite
, but when I switch to mysql
database it produces the following error:
I am using mysql 5.7.39
, is there any other database settings I need to make to eliminate this problem?
@yzxh24 Hi! Yes, there is a problem with MySQL, and it seems to occur because MySQL doesn't know which column to choose for aggregation, given that both 'exam_questions.id'
and 'questions.id'
are present.
As a quick fix solution you can change your QuestionsRelationManager.php from
$query->sum('score'))
to
$this->getOwnerRecord()->questions()->sum('questions.score'))
@dmitry-udod Yes, just add the table name, since what I need sum
for is the score
of the exam_qustions
table, so it should be changed to:
Tables\Columns\TextInputColumn::make('pivot.score')
->summarize(Tables\Columns\Summarizers\Sum::make())
/// The exam_questions table is used here
->using(fn (Builder $query): string => $this->getOwnerRecord()->questions()->sum('exam_questions.score'))
)
Testing on mysql
has worked fine.
Thanks again for your help!
Package
filament/filament
Package Version
v3.2.66
Laravel Version
v10.48.7
Livewire Version
No response
PHP Version
PHP 8.2.0
Problem description
When
BelongsToMany
is used for intermediate table,summarize()
cannotsum
the intermediate table fields correctly.In mysql , The error message looks like this:![WeChat746e8a1b4c7f5cbc3b06d747dc6e0112](https://github.com/filamentphp/filament/assets/5319741/c7a1fe53-3548-45ef-99e6-ff1643b8f171)
In sqlite, the error message looks like this:![WeChat3d1b3f07127a0c2c335fd6379c260cd6](https://github.com/filamentphp/filament/assets/5319741/4bff6adc-9c4b-467e-a330-29c5f1897d39)
Expected behavior
I want summarize() to count the data correctly.
Steps to reproduce
I have created three tables as follows:
model as follows:
When I use
RelationManager
to manage associations, I get an error when usingTables\Columns\Summarizers\Sum::make()
intable()
:Reproduction repository
https://github.com/yzxh24/filament-issue
Relevant log output
No response