Laravel-如何在Laravel口才搜索中应用GroupBy

时间:2019-06-02 19:48:59

标签: laravel eloquent

我正在使用Laravel雄辩地按条件搜索。问题是,在我下面编写的查询中,我想使用GroupBy名称的count(plan)和sum(amount)汇总

控制器

    public function index(Request $request,$transaction_type)
    {
        if($transaction_type=='Income' || $transaction_type== 'Expense') {
            $data['title'] = $transaction_type . ' Details';
            $transaction = new Transaction();
            $transaction = $transaction->with('relTransactionHead','relUser');
            $transaction = $transaction->where('type', $transaction_type);
            $render = [];
            if (isset($request->transaction_id)) {
                $transaction = $transaction->where('transaction_id', 'like', '%' . $request->transaction_id . '%');
                $render['transaction_id'] = $request->transaction_id;
            }
            if (isset($request->client_name)) {
                $transaction = $transaction->where('client', 'like', '%' . $request->client_name . '%');
                $render['client_name'] = $request->client_name;
            }
            if (isset($request->transaction_head_id)) {
                $transaction = $transaction->where('transaction_head_id', $request->transaction_head_id);
                $render['transaction_head_id'] = $request->transaction_head_id;
            }
            $transaction = $transaction->orderBy('id', 'DESC');
            $transaction = $transaction->paginate(10);
            $transaction = $transaction->appends($render);
            $data['transactions'] = $transaction;
            $data['serial'] = $this->managePagination($transaction);
            $data['transaction_heads'] = TransactionHead::where('type', $transaction_type)->where('status', 'Active')->pluck('name', 'id');
            $data['transaction_type'] = $transaction_type;
            return view('admin.transaction.index', $data);
        }else{
            return redirect()->back();
        }
    }

型号


class Transaction extends Model
{
    public function relTransactionHead()
    {
        return $this->belongsTo('App\TransactionHead','transaction_head_id','id');
    }
    public function relUser()
    {
        return $this->belongsTo('App\User','client','id');
    }

}

查看

                    @foreach($transactions as $transaction)

                        <tr>
                            <td>{{ $serial++ }}</td>
                            <td>{{ $transaction->transaction_id }}</td>
                            <td>{{ $transaction->relTransactionHead->description }}</td>
                            <td>
                                    {{ $transaction->relUser->name }}

                            </td>
                            <td>{{ date('d M Y',strtotime($transaction->date)) }}</td>
                            <td>{{ $transaction->amount }}</td>
                            <td>{{ $transaction->plan }}</td>

                        </tr>
                    @endforeach

我想

  

count($ transaction-> plan)

     

sum($ transaction->金额)

,然后按$ transaction-> date和$ transaction-> relUser-> name分组

如何在查询中应用它?

0 个答案:

没有答案