dimanche 29 décembre 2019

How to get sum along with this Laravel Eloquent query

Database Structure:
Table: sales_payments
columns: id, payer_id, payment_status, amount , ...

Please see this eloquent query. it's working fine but now i need the sum of amount key along with this given eloquent query and where conditions.

    $query =  SalesPayment::with(['abc.xyz'])

        ->whereHas('abc.xyz', function ($query) use ($options) {
            $query->where('xyz_id',$options['xyz_id']);
        });

    $query->where(['key3' => key3(), 'payment_status' => PAYMENT_STATUS_SUCCESS]);
    $query->orderBy('created_at', 'desc');
    return $query->paginate(config('constants.PAGE_LIMIT'));

Possible Solution

Just put a select as mentioned below

$query =  SalesPayment::select('*', \DB::raw('SUM(amount) AS total_sale_amount')->with ....

I have tested this solution it's working fine.

Please let me know if there is a better solution than this. And I'm looking for some other solutions Also.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire