0

I have query with this

->leftjoin(
    DB::raw("(
        SELECT transactable_id AS invoice_id, transactions.payment_method_id, payment_methods.name AS payment_method, SUM(transactables.amount) AS trans_amount
        FROM transactions
        join payment_methods on payment_methods.id = transactions.payment_method_id
        join transactables on transactables.transaction_id = transactions.id
        and transactions.deleted_at is null
        and transactables.deleted_at is null
        and transactable_type = 'Sale'
        GROUP BY transactable_id

    ) AS trx"), 'trx.invoice_id', '=', 'sales.id', 'GROUP BY trx.invoice_id')
->addSelect(DB::raw('IFNULL(trans_amount , 0.00 ) AS trans_amount'),DB::raw('trx.payment_method'))

Return result from the select like

+"trans_amount": "200.00"
+"payment_method": "Cash"

How to form the data base on the value like

+"Cash": "200.00"
2
  • Not simply done, me thinks. vitobotta.com/2015/12/10/mysql-dynamic-column-name How might your results vary? Where is your table schema? Commented Mar 26, 2020 at 6:21
  • mysql questions are best accompanied with a db-fiddle demo link. Commented Mar 26, 2020 at 6:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.