MedicOneSystems / livewire-datatables

Advanced datatables using Laravel, Livewire, Tailwind CSS and Alpine JS
https://livewire-datatables.com/
MIT License
1.19k stars 259 forks source link

Can't mix 2 columns one is calculated and the other one is from the original table #369

Open abeer93 opened 2 years ago

abeer93 commented 2 years ago

Hello, I'm using Laravel framework and I installed livewire-datatables package to display my tables. I have 2 tables

            class Budget extends Model
            {
                protected $fillable = ['amount'];

                public function prizes(): HasMany
                {
                    return $this->hasMany(Prizes::class);
                }
            }

            class Prizes extends Model
            {
                protected $fillable = ['budget_id', 'budget'];

                public function budget(): BelongsTo
                {
                    return $this->belongsTo(Budget::class);
                }
            }

I'm trying to display list of budgets and I need to display the total amount of budget and the used amount of budgets in same columns. total amount of budget is represented from the "amount" column in budgets table. used amount of budget is calculated column represented from the sum of "budget" column from prizes table.

===> I tried to do this but it doesn't work

    Column::callback(['budgets.amount', 'prizes.budget:sum'], function ($amount, $budgetSum) {
                    return $budgetSum . "/" . $amount . "(" . $budgetSum / $amount . ")";
    })->label('Used Amount / Total Amount'),

Any Help?

danicsan commented 2 years ago

I have a similar problem

Column::callback(
                ['details.original_price:sum' , 'details.sale_price:sum'],

gives an error an error in the SELECT statement there is this

  CONCAT_WS('|**lwdt**|' ,COALESCE(, ''), COALESCE(, '')) AS `callback_154468063`

there is the complete SQL Query

  select (select COALESCE(sum(purchase_details.original_price),0) from `purchase_details` where `purchases`.`id` =
  `purchase_details`.`purchase_id`) as `callback_154468063`, 
  (select COALESCE(sum(purchase_details.sale_price),0) from `purchase_details` where `purchases`.`id` = `purchase_details`.`purchase_id`) as `callback_154468063`, 
  `purchases`.`id` as `id`,
  `purchases`.`unique_id` as `unique_id`,
  `users`.`email` as `user.email`, 
  CONCAT_WS('|**lwdt**|' ,COALESCE(customers.billing_first_name, ''), COALESCE(customers.billing_last_name, '')) AS `callback_2139183833`, 
  `payment_types`.`name` as `payment_type.name`,
  `purchases`.`payment_status` as `callback_2035336450`, 
  `purchases`.`status` as `callback_1538380264`, 
  CONCAT_WS('|**lwdt**|' ,COALESCE(, ''), COALESCE(, '')) AS `callback_154468063`, 
  CONCAT_WS('|**lwdt**|' ,COALESCE(customers.delivery_first_name, ''), COALESCE(customers.delivery_last_name, '')) AS `callback_1527639191`, 
  `purchases`.`shipping_price` as `callback_3031125171`, 
  `purchases`.`shipping_type` as `shipping_type`, 
  `purchases`.`created_at` as `created_at`, 
  `purchases`.`id` as `callback_3663396338` 
  from `purchases`
  left join `customers` on `customers`.`user_id` = `purchases`.`id` 
  left join `users` on `customers`.`user_id` = `users`.`id` 
  left join `payment_types` on `purchases`.`payment_type_id` = `payment_types`.`id` order by `id` desc limit 10 offset 0