F
Filamentβ€’4mo ago
tumIkuwye

SQL Syntax Alias Error When Using Table Summarizer

My case is because I have multiple database connection on my application. So in every model I need to use:
public function getTable(): string
{
return "{$this->getConnection()->getDatabaseName()}.mytable";
}
public function getTable(): string
{
return "{$this->getConnection()->getDatabaseName()}.mytable";
}
I did this because somehow laravel will use wrong connection for any feature. But when I am using summarize on the Table Builder like this:
Tables\Columns\TextColumn::make('bill')
->summarize([
Tables\Columns\Summarizers\Average::make(),
Tables\Columns\Summarizers\Range::make(),
Tables\Columns\Summarizers\Sum::make(),
])
->numeric(2),
Tables\Columns\TextColumn::make('bill')
->summarize([
Tables\Columns\Summarizers\Average::make(),
Tables\Columns\Summarizers\Range::make(),
Tables\Columns\Summarizers\Sum::make(),
])
->numeric(2),
It will return sql error like this:
select
avg(db2.mytable.bill) as "Cu4djg5yPhCx6VQ6",
sum(db2.mytable.bill) as "j3nj8trVJa0xc7JI"
from
(
select
*
from
`db2`.`mytable`
where
(
month(`billing_date`) = 07
and year(`billing_date`) = 2024
)
order by
`db2`.`mytable`.`id` asc
limit
10 offset 0
) as `db2`.`mytable`
select
avg(db2.mytable.bill) as "Cu4djg5yPhCx6VQ6",
sum(db2.mytable.bill) as "j3nj8trVJa0xc7JI"
from
(
select
*
from
`db2`.`mytable`
where
(
month(`billing_date`) = 07
and year(`billing_date`) = 2024
)
order by
`db2`.`mytable`.`id` asc
limit
10 offset 0
) as `db2`.`mytable`
It uses alias "AS db2.mytable" thats why it got me error, but if I comment my getTable() function on my Model it runs okay. So I am gonna comment my getTable function for now. Hopefully someone know any solution, because I do still need getTable to prevent the application from using wrong db otherwise it is either alias error or wrong db error. πŸ™πŸ«‘
1 Reply
Lara Zeus
Lara Zeusβ€’4mo ago
why you dont use the protected $connection = 'connectionName'; on the model instead of setting it on the getTable
Want results from more Discord servers?
Add your server