How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Add an HTML Column in Laravel DataTables
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Add an HTML Column in Laravel DataTables
Web Development

How to Add an HTML Column in Laravel DataTables

how7o
By how7o
Last updated: May 10, 2026
7 Min Read
Laravel DataTables HTML column — rawColumns opt-out of the default escaping
SHARE

The laravel datatables html column renders as escaped text (<a href>) instead of a working link because Yajra’s DataTables server-side integration escapes every column by default. The fix is ->rawColumns(['column_name']), which whitelists specific columns to be emitted as raw HTML. This guide covers the one-line fix, when to add rawColumns versus render Blade inside the callback, and how to keep the escaping-off columns safe from XSS.

Contents
  • TL;DR
  • Why the default escapes HTML
  • The minimal fix
  • Multiple HTML columns
  • Keep XSS-safe inside a raw column
  • For larger HTML — render a Blade partial
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on Laravel 11 with PHP 8.3 and yajra/laravel-datatables-oracle v11. Originally published 2023-03-30, rewritten and updated 2026-04-23.

TL;DR

After adding an HTML column with addColumn, chain ->rawColumns([...]) before ->make():

return Datatables::of($users)
    ->addColumn('edit', function ($row) {
        return '<a href="' . route('user.edit', $row->id) . '">Edit</a>';
    })
    ->rawColumns(['edit'])
    ->make(false);

Why the default escapes HTML

Yajra’s DataTables is a server-side responder: the client asks for a page of rows via AJAX, Laravel produces JSON with the row data, and the client renders it. Every column value in that JSON goes through HTML escaping before it’s sent, because the common case — a user’s name column, a description — contains untrusted text that could include <script> tags. Escaping by default is the right behavior for untrusted data.

But when you’ve built the HTML yourself inside an addColumn callback — an Edit link, a delete button, a status badge — escaping turns your tags into visible text: <a href="/users/10/edit">Edit</a> shows up literally in the table cell. rawColumns is the opt-out: “trust these specific columns as-is.”

The minimal fix

Add one line. The column name in rawColumns must match the name you passed to addColumn:

return Datatables::of($users)
    ->addColumn('edit', function ($row) {
        return '<a href="' . route('user.edit', $row->id) . '">Edit</a>';
    })
    ->rawColumns(['edit'])
    ->make(false);

Everything else — name, email, whatever database columns the table is showing — stays escaped. Only edit is emitted as raw HTML, and the browser renders the <a> tag properly.

laravel datatables html column — rawColumns opts specific columns out of the default escape

Multiple HTML columns

Pass every column you want raw in the array — edit, delete, status badge, avatar:

return Datatables::of($users)
    ->addColumn('avatar', fn ($row) => '<img src="' . $row->avatar_url . '" class="avatar">')
    ->addColumn('status', fn ($row) => $row->active
        ? '<span class="badge bg-success">Active</span>'
        : '<span class="badge bg-secondary">Inactive</span>')
    ->addColumn('actions', fn ($row) => view('users._actions', compact('row'))->render())
    ->rawColumns(['avatar', 'status', 'actions'])
    ->make(false);

Forgetting to add a column to rawColumns is the most common reason one cell renders as escaped text while its neighbors look fine.

Keep XSS-safe inside a raw column

rawColumns turns off automatic escaping for the whole column, so any database value you interpolate into the HTML string is shipped verbatim. Wrap anything user-authored with Laravel’s e() helper:

->addColumn('name_link', function ($row) {
    return '<a href="' . route('user.show', $row->id) . '">'
        . e($row->name)              // user-supplied — escape it
        . '</a>';
})
->rawColumns(['name_link'])

e() is Laravel’s wrapper around htmlspecialchars with sane defaults (double-quote escape, UTF-8). Structural tags (<a>, <span>) stay as tags because you wrote them; the content you pulled from the database gets escaped before interpolation.

For larger HTML — render a Blade partial

Once the HTML inside a callback goes beyond a single tag, PHP string concatenation gets painful. Render a Blade view instead:

->addColumn('actions', function ($row) {
    return view('users._row_actions', ['row' => $row])->render();
})
->rawColumns(['actions'])
{{-- resources/views/users/_row_actions.blade.php --}}
<div class="btn-group">
    <a href="{{ route('user.edit', $row->id) }}" class="btn btn-sm btn-primary">Edit</a>
    <form method="POST" action="{{ route('user.destroy', $row->id) }}" class="d-inline">
        @csrf
        @method('DELETE')
        <button class="btn btn-sm btn-danger">Delete</button>
    </form>
</div>

Blade’s {{ ... }} auto-escapes user content, so you get the XSS guard for free — no manual e() calls. This is the cleanest pattern once the action column has more than one button or any per-row conditional rendering.

Frequently asked questions

Why is my laravel datatables html column rendering as escaped text?

Because Yajra’s DataTables integration escapes every column’s output by default — a safety default that protects you from XSS when the column data came from the database. When you’ve built an HTML string yourself (an Edit button, a status badge, an icon), tell DataTables which columns you trust with ->rawColumns(['column_name']). Only those columns are emitted as raw HTML; everything else stays escaped.

What goes inside the rawColumns array?

The same column names you used in addColumn or editColumn — the keys that the frontend sees. If you added an edit column and an actions column, call ->rawColumns(['edit', 'actions']). Existing database columns stay escaped unless you explicitly list them too.

Is it safe to put database content in a rawColumns entry?

Only if you’ve escaped the user-supplied parts yourself. The point of rawColumns is to opt out of automatic escaping, so any variable you interpolate is shipped verbatim to the browser. Use e($row->name) (Laravel’s htmlspecialchars wrapper) inside the callback around anything the user wrote, and only leave the structural HTML (<a>, <span class="badge">) unescaped.

Should I use make(false) or make(true)?

make(false) does not mangle the raw columns; make(true) additionally runs PHP’s htmlentities on all remaining output, which is largely redundant because DataTables already escapes non-raw columns. Most current Yajra\DataTables code just calls ->make() with no argument — the library defaults are already the safer choice. Keep make(false) if you’re following an existing pattern in a codebase; there’s no gotcha.

Can I reuse the same HTML snippet across rows via a Blade partial?

Yes — render a Blade view inside the callback: return view('datatables.actions', ['row' => $row])->render(). Blade auto-escapes interpolations ({{ $row->name }}) while leaving tags alone, so you get safe concatenation without manually calling e(). This is cleaner than long PHP concatenation once the HTML grows beyond a single tag.

Related guides

  • How to Search Custom or Composite Columns in Laravel DataTables — the sibling problem of search on columns addColumn produces.
  • How to Fix “Unknown column ‘CONCAT'” in Laravel — why building composite values needs DB::raw.
  • How to Use Multiple where and orWhere in Laravel Eloquent — filtering the DataTable data source.
  • How to Install Laravel on Ubuntu — a fresh project to add Yajra DataTables to.

References

Yajra\\DataTables docs (raw columns, escaping): yajrabox.com/docs/laravel-datatables.

TAGGED:datatablesjQueryLaravelphp

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Laravel request inputs with prefix — filter request()->all() by Str::startsWith How to Retrieve Inputs with a Specific Prefix in Laravel Request
Next Article Laravel DataTables custom column search — filterColumn callback handles the search SQL How to Search Custom or Composite Columns in Laravel DataTables
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026

You Might Also Like

Fix Laravel CSRF token mismatch in DataTables Ajax
Web Development

How to Fix Laravel CSRF Token Mismatch in DataTables Ajax

4 Min Read
Laravel unknown column CONCAT fix — DB::raw and selectRaw bypass identifier escaping
Web Development

How to Fix “Unknown column ‘CONCAT'” in Laravel

8 Min Read
Enforce a minimum phone-number length in WooCommerce checkout
Web Development

How to Set a Minimum Length for Phone Numbers in WooCommerce

6 Min Read
Submit a form with jQuery
Web Development

How to Submit a Form Using jQuery

4 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?