Jim
Jim

Reputation: 622

Datatables - Yajra returning an incorrect string - Laravel 5.8

I am builing a rather simple page listing UK pubs. There are over 200,000 of them.

I have written a straightforward blade template with datatables being called by ajax to a route in a controller DatatablesController. All works well but I want to add edit and view buttons, so here is my controller function:

  public function getPubs(Request $request)
      {
        $model = Pub::query();
        return DataTables::eloquent($model)
                 ->addColumn('action', function($row){
                           $btn = '<a href="javascript:void(0)" class="edit btn btn-primary btn-sm">View</a>';
                            return $btn;
                    })
                ->addColumn('edit', function($row){
                          $editbtn = '<a href="javascript:void(0)" class="edit btn btn-primary btn-sm">edit</a>';
                           return $editbtn;
                   })
                ->make(true);
      }

The problem is the edit column is returning an incorrect string. Here is the end of the ajax result of the first item:

    "action": "<a href=\"javascript:void(0)\" class=\"edit btn btn-primary btn-sm\">View</a>",
    "edit": "&lt;a href=&quot;javascript:void(0)&quot; class=&quot;edit btn btn-primary btn-sm&quot;&gt;edit&lt;/a&gt;"

I cannot see any reason in my method why this is not correctly returned.

Upvotes: 1

Views: 800

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58880

Use rawColumns() API method to prevent Laravel DataTables from encoding HTML entities.

For example:

return DataTables::eloquent($model)
   ->addColumn('action', function($row){
      $btn = '<a href="javascript:void(0)" class="edit btn btn-primary btn-sm">View</a>';
      return $btn;
   })
   ->addColumn('edit', function($row){
      $editbtn = '<a href="javascript:void(0)" class="edit btn btn-primary btn-sm">edit</a>';
      return $editbtn;
   })
   ->rawColumns(['action', 'edit'])
   ->make(true);

Upvotes: 1

Related Questions