how to use yajrabox DataTables with the Laravel framework to render images in a DataTable.
Step 1: Install Required Packages
Make sure you have a Laravel project set up and running. Then, install the required packages using Composer and npm:
Step 2: Configure DataTables
In your config/datatables.php configuration file, set the default driver to "datatablesbuttons":
Step 3: Create Admin Model and Migration
Create an Admin model and corresponding migration to store admin information, including the profile_picture field:
In the migration file, add the profile_picture field:
Run the migration to create the admins table:
Step 4: Seed Sample Data (Optional)
If you want to test with some sample data, you can create a seeder to populate the admins table:
php artisan make:seeder AdminSeeder
In the seeder, add sample data including image URLs:
<?php
// database/seeders/AdminSeeder.php
use App\Models\Admin;
use Illuminate\Database\Seeder;
class AdminSeeder extends Seeder
{
public function run()
{
Admin::create([
'name' => 'John Doe',
'email' => 'john@example.com',
'profile_picture' => 'http://example.com/sample1.jpg',
]);
Admin::create([
'name' => 'Jane Doe',
'email' => 'jane@example.com',
'profile_picture' => 'http://example.com/sample2.jpg',
]);
// Add more sample data as needed...
}
}
php artisan db:seed --class=AdminSeeder
Step 5: Create DataTable Class
Create a new AdminDataTable class that extends the Yajra DataTable class:
php artisan make:data-table AdminDataTable
In the AdminDataTable class, define the dataTable and query methods to handle the DataTable configuration and data retrieval:
<?php
// app/DataTables/AdminDataTable.php
namespace App\DataTables;
use App\Models\Admin;
use Illuminate\Support\HtmlString;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;
class AdminDataTable extends DataTable
{
public function dataTable($query)
{
return datatables()
->eloquent($query)
->addColumn('image', function (Admin $admin) {
return new HtmlString('<img src="' .
$admin->profile_picture . '" width="100"
height="100">');
})
->addColumn('action', 'admin.action');
}
public function query(Admin $model)
{
return $model->newQuery();
}
public function html()
{
return $this->builder()
->setTableId('admin-table')
->columns($this->getColumns())
->minifiedAjax()
->dom('Bfrtip')
->orderBy(0)
->buttons([
'excel',
'csv',
'pdf',
'print',
'reset',
'reload',
]);
}
protected function getColumns()
{
return [
Column::make('id'),
Column::make('image')->title('Profile Picture'),
Column::make('name'),
Column::make('email'),
Column::make('created_at'),
Column::make('updated_at'),
Column::computed('action')
->exportable(false)
->printable(false)
->width(60)
->addClass('text-center'),
];
}
}
Step 6: Create a Route to Render the DataTable
In your routes/web.php file, define a route to render the DataTable:
<?php
use App\DataTables\AdminDataTable;
Route::get('/admins', function (AdminDataTable $dataTable) {
return $dataTable->render('admin.index');
});
Step 7: Create a Blade View
Create a Blade view file admin.index.blade.php to display the DataTable:
// resources/views/admin/index.blade.php
@extends('layouts.app')
@section('content')
<h1>Admins DataTable</h1>
{!! $dataTable->table(['class' => 'table table-bordered table-striped']) !!}
@endsection
@push('scripts')
{!! $dataTable->scripts() !!}
@endpush
Step 8: Render the DataTable
Finally, visit the /admins route in your browser, and the DataTable with image rendering should be displayed.

![[SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale [SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4EweEaEcbKOxq_O5HgcG9TLn1F4KvQG0VWEaDsM4LdvxGM0BQsbsPipcsxWc8DMrHQX5Gqj1Jb50JiC7SkbK0XDhc2e0q6Jl8soRhP-Jmdydv_wQovCrKLHbuy1w7iHv5A3dU4Y6LEmLJevhbwdcwj5bwNxJ7H0XRRBETP3MFnmshjV_amyyXKn35vLw/w640-h360-rw/Modern%20Promotion%20Business%20Agency%20Blog%20Banner.png)
![[SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale [SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRYSVgj2De0-KyDH2DGBcrmc7d4zspIAolcFOZ2EHtG22iFOG9L7w3SLapMass1OSJPQuarocnmTdvPS6LqXetQE8eqv3hsdHWjATlQBajVp-NUDsSKW9UxTq9zXLkTd8AJySxQrI4WfNw1gJ7iVfgKLt7jSZkjNYRcKY9rEN2Q7JIjevLeoLtqlcYhPE/w640-h272-rw/npm-command-datatale.png)
![[SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale [SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCdeeC-DdDjZiEsLTynoVt-RDnVUWaYaXoalAq8WY-hJxkhrTz-wvAipiy2MVXTHlWdeYq0oGFKIRONzycgXMU6BBf4nHzKNYaAKDJ1ODs3Gs3U8b-jvrgvCRV-aZjpcwgTw4Kn46pucmM1toQbPFmrI3dlTNXTaT52BR5hdfYuGvxLR3StP9er1iCCic/w640-h174-rw/yajrabox-saprty-config-datatable.png)
![[SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale [SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7cloUjFs2hG3wzrjbE71IeLQfQ6NFoizpFCA-DvbYd7AUuiZ7xdFETkHtcR6BgDuTu28GpsUjn4Y1V1iFMorrxkzAUi_NmLq2hrhoPRdow90NRzRqd51umaUf5ZrnTWlR-NiMuBu41_NNZwPzC1Vz3_YyG9mXpSHE0rly-5tKTPBhTgf9WIb3XjdFQj4/w640-h122-rw/command-install.png)
![[SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale [SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAALR7N8XUmTr4dVtuL56_IDyQfWAsgCRFC1KR45vhp1B0qShrunrmKOBQ1Fd_lal2KUiVBiZHaed_J7k7ai-W0oaeGwGD4UMacNHV5yfHoLhXBifUB4biL0HBflhmSr8d_1UNNp0pmG-y0iqTQ7hkrY82LV4EqZKvy1weKfJK295bDESSToc1rY33OKo/w640-h402-rw/migretion-table-admin.png)
![[SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale [SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhF6Y7UgURTZzjuxT_dA4oiWCmSNIxvhN5cOCXtXdz_-8OeKGtmkioiR0SLfdwTClvw07Apy-ASN7tJdc7VsCT0Sg2I3aD20biKx0NnPxk65RLc8aujJS44sy_jkFxb9In5tvAl4ykReijFsEpV_lsNDpuIGYIWJpbvCfF3zlTw3n_FXsRRF-F-BBcEAwE/w640-h136-rw/php-artisan-migrate.png)
![[SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale [SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTCH5FCm4EHx-1SpPWbpuzjq2iqWPRCXO5OA6c0TL2fuSWd6NVKwZ1W82j-Nw4_-7xceLbNWLP65NJ0OZ2Qfc0Zy4HFqDxSDYXZNDah9CPcRSggCbe7ErL8efjtXRCXH3H2HWqMydsytQO9cL8g0J3clJSZkZx9lb5YydMPELG1JgF5NTLGroPpW052Ck/w229-h640-rw/AdminDataTable.png)
![[SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale [SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbdNgf2alC0eYmqJSauDw0EK_dSrmyIY67cDwW8oiMz-2aP85vUtIGYhSGwtGaA_UMR0cjuxmXBxCOaTu9Q3PZAX0IMG_hrJP1PXTW0kAj8f5XdNBw8OLZwWJFICcfyUsoIwW3yDwrf8wSSacWlol1dWQWKdBy6xpZvGUIlL204TjG3eLo6ns4QB3fiv0/w494-h640-rw/AdminSeeder.png)
![[SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale [SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAQQ04i76YNKzo6de_zALy6Rv9auVWyjl4JNFiZQ251AkKctXzb_qlSIFmQA5e3Qq_S3h3klqOfzRLMwZOZ1LuAolZIej3XCWdGZ9co09icc9pLtmQyL-uThAM2zw40BeXcvO-CCAjFsBShFFSY665YMxQ5ehXn8qP_J4aSJD8qLohS6IKmQ5fmx5g58k/w640-h482-rw/resources-views.png)
![[SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale [SOLVED ERROR] yajrabox column image add | sparti datatable image rendring full exmpale](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIbOW6bNXaawEAkodlr7JX0YwFuB_MY5VSQyU17By1r38bDFyU4QWK5i6z-JMij4iaeyvYMXyQV9qwbHosMbdb6Y7kSOa9wkFZz3mwVgh0-zDRqonI9wRBYZi7Y-EhX7GRIRtODxffvoLzPjRcQdwf2kt0SXWrv5Kip8hfyCSzwsquT5UOZjNPmKPosSM/w640-h270-rw/Route.png)