You only have two tables but use belongsToMany. This requires a pivot table. Did you want to use a hasMany perhaps?
Nov 26, 2022
9
Level 1
Get data in eloquent "withSum()"
I have tried to use withSum according to the documentation. But I think I am making a mistake in thinking.
I have two tables wallet_data wallet_data_transaction (with key wallet_data_id)
I have try this:
$wallet_datas = WalletData::where('....', 0)
->withSum('wallet_data_transactions', 'fees')
->get()
And in my WalletData Model:
public function wallet_data_transactions()
{
return $this->belongsToMany(WalletDataTransaction::class, 'wallet_data_transactions'');
}
The Error
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'wallet_data_transactions'
It is clear what the error means, but not where it is triggered. What have I misunderstood?
I am a little confused why Laravel generates this query:
select wallet_datas.*,
(
select sum(wallet_data_transactions.fees)
from wallet_data_transactions
inner join wallet_data_transactions on wallet_data_transactions.id = wallet_data_transactions.wallet_data_transaction_id
where wallet_datas.id = wallet_data_transactions.wallet_data_id
) as wallet_data_transactions_sum_complete_fees
from wallet_datas where ......
Why is there an inner join?
Level 102
1 like
Please or to participate in this conversation.