PHP code example of sejator / ci4-datatables

1. Go to this page and download the library: Download sejator/ci4-datatables library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sejator / ci4-datatables example snippets


use Sejator\DataTables\DataTables;

return DataTables::from('users')
    ->select('id, name, email, status')
    ->searchable(['name', 'email']) // 🔒 whitelist kolom search
    ->make();

return DataTables::from('users')
    ->select('id, name, email, status')
    ->where('status', 'active')
    ->searchable(['name', 'email'])
    ->make();

$status = $this->request->getGetPost('status');

return DataTables::from('users')
    ->select('*')
    ->when($status, fn ($q, $v) => $q->where('status', $v))
    ->searchable(['name', 'email'])
    ->make();

  return DataTables::from('users')
    ->select('
        users.name AS nama,
        users.role AS role
    ')
    ->orderable([
        'nama' => 'users.name',
        'role' => 'users.role'
    ])
    ->make();
  

return DataTables::from('users')
    ->select('id, name, email, status')
    ->where('status', 'active')
    ->searchable(['name', 'email'])
    ->debug()
    ->make();

DataTables::from('users')
    ->select('*')
    ->where('status', 'inactive')
    ->ddSql();

DataTables::from('users')
    ->select('*')
    ->where('status', 'pending')
    ->logSql()
    ->make();

->searchable(['name', 'email'])

$dt = DataTables::from('users');

$dt->where('status', 'active')->make();

$dt->reset()
   ->where('status', 'inactive')
   ->make();

return DataTables::from('izin')
    ->select('id, tanggal_mulai, status')
    ->withRelation(
        'izin_id',     // foreign key di tabel relasi
        'id',          // primary key di tabel utama
        'izin_histori',
        'izin_id, tanggal'
    )
    ->make();

$row['izin_histori'] = [
    ['izin_id' => 1, 'tanggal' => '2024-01-01'],
    ['izin_id' => 1, 'tanggal' => '2024-01-02'],
];

return DataTables::from('izin')
    ->select('id, status')
    ->withRelation(
        'izin_id',
        'id',
        'izin_histori',
        'izin_id, tanggal',
        [
            'onlyIfUsedIn' => ['tanggal']
        ]
    )
    ->addColumn('tanggal', function ($row) {
        return implode(
            ', ',
            array_column($row->izin_histori, 'tanggal')
        );
    })
    ->make();

return DataTables::from('izin')
    ->select('id, status')
    ->withRelation(
        'izin_id',
        'id',
        'izin_histori',
        'id, izin_id, tanggal',
        [
            'nested' => [
                [
                    'foreignKey' => 'izin_histori_id',
                    'localKey'   => 'id',
                    'table'      => 'izin_histori_detail',
                    'columns'    => '*',
                ]
            ]
        ]
    )
    ->make();

$row->izin_histori[0]['izin_histori_detail']