首页 > 解决方案 > Laravel Livewire 排序国外领域

问题描述

嗨,我有两张表客户和费用我想要的是对第三个字段进行排序,该字段将在客户 foreach 刀片上保持平衡

我的刀片:

   <th  wire:click="sortBy('total_charges')" >Charge </th>
                           
   <th wire:click="sortBy('total_payments')" >Payments</th>
                           
  <th wire:click="sortBy('services_count')">balance</th>

 <td>{{ $customer->total_charges}}&euro; </td>
 <td>{{ $customer->total_payments}}&euro;</td>
 <td>{{ $customer->total_payments - $customer->total_charges}}&euro;</td>

当然,我在 foreach 中拥有与客户表相关的其他数据

我的qyery:

    public function render()
    {
        $customers = Customer::query()
       ->search($this->search)
       //->selectRaw('*, total_charges - total_payments as balance',)
       ->withSum('charges as total_charges','taskscharges')
       ->withSum('charges as total_payments','payment')
       
       ->withCount('charges')  
       ->orderBy($this->sortBy,$this->sortDirection)
       ->paginate($this->perPage);
 

        return view('livewire.customers.show',['customers'=>$customers 
        ])
        ->extends('layouts.maindashboard')
        ->section('content');
    }

如您所见,我注释掉了->selectRaw('*, total_charges - total_payments as balance',)因为字段> total_charges 和total_payments 是外部表的总和的错误,所以有什么简单的方法可以识别为余额来进行排序吗?

标签: laraveleloquentlaravel-livewire

解决方案


推荐阅读