How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: How to Add an HTML Column in Laravel DataTables
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > 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.
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

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Display PHP errors — ini_set + php.ini configuration
How to Display PHP Errors
May 10, 2026
PHP convert string to uppercase — strtoupper and mb_strtoupper
How to Convert a String to Uppercase in PHP
May 10, 2026
PHP string to float conversion with cast, regex cleanup, NumberFormatter
How to Convert a String to Float in PHP
May 10, 2026
PHP merge arrays without duplicates — union operator and array_unique
How to Combine Two Arrays Without Duplicates in PHP
May 10, 2026
PHP delete array element — unset, array_splice, array_filter, array_search
How to Delete an Element from a PHP Array
May 10, 2026

You Might Also Like

Install Laravel on Ubuntu — terminal with composer create-project command and Laravel red-pillar icon
Web Development

How to Install Laravel on Ubuntu: Step-by-Step Guide

9 Min Read
WordPress admin notice — four notice types shown at the top of the admin area
Web Development

How to Show Custom Notifications in the WordPress Dashboard

6 Min Read
Check if Laravel scheduler is running (cron + php artisan schedule:run)
Web Development

How to Check if Laravel Scheduler Is Running (Cron + Logs)

6 Min Read
Install Node.js on Ubuntu — terminal with NodeSource setup_22.x curl command and Node.js hexagon icon
Web Development

How to Install Node.js on Ubuntu (22.04 & 24.04): Step-by-Step

11 Min Read
How7o

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

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?