'HTML returning as text Laravel Controller
I am trying to return HTML from Laravel controller, but it is returning as plan text:
Example: <input type="checkbox" class="row-select" value="9">
->addColumn('mass_delete', function ($row) {
return '<input type="checkbox" class="row-select" value="' . $row->id .'">' ;
})
->editColumn('installation_status', function ($row) {
return ($row->installation_status) ? "<span class='label bg-green'>Required</span>" : "<span class='label bg-red'>Not Required</span>";
})
Solution 1:[1]
By default, Blade {{ }} statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks. If you do not want your data to be escaped, you may use the {!! !!} syntax:
ForExample
Cntroller.php
public function index()
{
$html = "<b>Hello</b>";
return view('welcome', compact('html));
}
welcome.blade.php
<html>
<body>
{{ $html }}
<br/>
{!! $html !!}
</body>
</html>
Result
<b>Hello</b>
Hello //but bold
Solution 2:[2]
Looking at this, you are probably using Laravel Datatables. If that's the case, in order to render HTML in the table itself, you need to use rawColumns() function:
->addColumn('mass_delete', function ($row) {
return '<input type="checkbox" class="row-select" value="' . $row->id .'">' ;
})
->editColumn('installation_status', function ($row) {
return ($row->installation_status) ? "<span class='label bg-green'>Required</span>" : "<span class='label bg-red'>Not Required</span>";
})
->rawColumns(['mass_delete', 'installation_status']);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Abdul Haseeb Khan |
Solution 2 | zlatan |