0

i'm got stuck for 2 days, i got confused how to render column usind yajra/laravel-datatable

i'm using postgre as database, and yajra/laravel-datatables as package.

i have query builder like this

$data = DB::select("SELECT * FROM get_list_count_amount_transaction('chat', 'done', '2019-03-01', '2021-12-31' )")[0];

which generates a value of object : (i use var_dump() to see the value)

{ ["list_data"]=> string(2171) 
    "[{"id":"44649ccd-9195-4964-b48c-ed2098077dc5","kd_join":1,"status":"done","booking_date":"2021-04-18","transaction_type":"chat","price":4000.00,"date_create":"2021-04-18T19:56:57"},
      {"id":"e500d2c1-99ae-4436-8ecc-8073f4f05bba","kd_join":1,"status":"done","booking_date":"2021-03-20","transaction_type":"chat","price":10000.00,"date_create":"2021-03-19T21:41:41"}
     ]"
  ["count_transaction"]=> int(12) 
  ["total_amount_transaction"]=> string(9) "160500.00" 
}

i'm confused, how to render list_data into a table using datatble

this is my html builder function :

    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())

            ->minifiedAjax()
            ->addAction(['width' => '200px'])
            ->addColumnBefore([
                'defaultContent' => '',
                'data' => 'DT_Row_Index',
                'name' => 'DT_Row_Index',
                'title' => 'No',
                'render' => function () {
                    return 'function(data,type,fullData,meta){return meta.settings._iDisplayStart+meta.row+1;}';
                },
                'orderable' => false,
                'searchable' => false,
                'exportable' => false,
                'printable' => true,
                'footer' => '',
            ]);
    }

    protected function getColumns()
    {
        return [
            ['data' => 'transaction_type', 'name' => 'transaction_type', 'title' => 'Tipe Transaksi', 'orderable' => false],
            ['data' => 'status', 'name' => 'status', 'title' => 'Status', 'orderable' => false],
            ['data' => 'booking_date', 'name' => 'booking_date', 'title' => 'Tanggal Booking', 'orderable' => false],
            ['data' => 'price', 'name' => 'price', 'title' => 'Jumlah', 'orderable' => false],
        ];
    }
}
5
  • You dont need to use the html builder. datatables.yajrabox.com/collection/basic Commented May 6, 2021 at 13:11
  • but how to render only value on list_data? Commented May 6, 2021 at 13:15
  • still got error :( Commented May 6, 2021 at 13:18
  • The var_dump result you provided shows that list_data contains a string. It seems your data is saved as JSON. To use it you can do something with json_decode: $listDataArray = json_decode($data["list_data"]); Commented May 6, 2021 at 13:40
  • i have already doing that, but still got internal sever error in my console Commented May 6, 2021 at 13:52

1 Answer 1

1

Just grab the list_data, use json_decode and laravel collection to make your needed structure. Try to use map, filter, mapWithKeys or whatever suitable. For Instance:

collect(json_decode($listData))->mapWithKeys(function($item)){

      //
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.