laravelserializationdatatablenumbersrow

How to add row number or Serial no in laravel datatable


This is the bill_info table, for which i need to serialized row no like 1 2 . . . . . . . . . . . . .n

enter image description here

There is data list returned, how I can get serial_no custom field in datatable list view.

    $data = BillInfo::get(['bill_info.*']);

    return Datatables::of($data)
                    ->removeColumn('id')
                    ->make(true);

Solution

  • Set the variable rownum at the beginning of your query. Then set the increment process in your query.

    DB::statement(DB::raw('set @rownum=0'));
    
    $data = BillInfo::get(['bill_info.*', 
                        DB::raw('@rownum  := @rownum  + 1 AS rownum')]);
    
    return Datatables::of($data)
                    ->removeColumn('id')
                    ->make(true);
    

    Here you can get rownum as serial no of the given records [1 . . . 8].