PHP code example of schoppax / eloquent-datatable

1. Go to this page and download the library: Download schoppax/eloquent-datatable 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/ */

    

schoppax / eloquent-datatable example snippets


$users = new Models\User();
$dataTable = new LiveControl\EloquentDataTable\DataTable($users, ['email', 'firstname', 'lastname']);
echo json_encode($dataTable->make());

$dataTable->setVersionTransformer(new LiveControl\EloquentDataTable\VersionTransformers\Version109Transformer());

use LiveControl\EloquentDataTable\DataTable;

class UserController {
  ...
  public function datatable()
  {
    $users = new User();
    $dataTable = new DataTable(
      $users->where('city', '=', 'London'),
      ['email', 'firstname', 'lastname']
    );
    
    return $dataTable->make();
  }
}

use LiveControl\EloquentDataTable\DataTable;

class UserController {
  ...
  public function datatable()
  {
    $users = new User();
    $dataTable = new DataTable(
      $users,
      ['email', ['firstname', 'lastname'], 'city']
    );
    
    return $dataTable->make();
  }
}

use LiveControl\EloquentDataTable\DataTable;
use LiveControl\EloquentDataTable\ExpressionWithName;

class UserController {
  ...
  public function datatable()
  {
    $users = new User();
    $dataTable = new DataTable(
      $users,
      [
        'email',
        new ExpressionWithName('`id` + 1000', 'idPlus1000'),
        'city'
      ]
    );
    
    return $dataTable->make();
  }
}

use LiveControl\EloquentDataTable\DataTable;

class UserController {
  ...
  public function datatable()
  {
    $users = new User();
    $dataTable = new DataTable($users, ['id', ['firstname', 'lastname'], 'email', 'city']);
    
    $dataTable->setFormatRowFunction(function ($user) {
      return [
        $user->id,
        '<a href="/users/' . $user->id . '">' . $user->firstnameLastname . '</a>',
        '<a href="mailto:' . $user->email . '">' . $user->email . '</a>',
        $user->city,
        '<a href="/users/delete/' . $user->id . '">&times;</a>'
      ];
    });
    
    return $dataTable->make();
  }
}

use LiveControl\EloquentDataTable\DataTable;

class UserController {
  ...
  public function datatable()
  {
    $users = new User();
    $dataTable = new DataTable(
    	$users->with('country'),
    	['name', 'country_id', 'email', 'id']
    );
    
    $dataTable->setFormatRowFunction(function ($user) {
    	return [
    		'<a href="/users/'.$user->id.'">'.$user->name.'</a>',
    		$user->country->name,
    		'<a href="mailto:'.$user->email.'">'.$user->email.'</a>',
    		'<a href="/users/delete/'.$user->id.'">&times;</a>'
    	];
    });
    
    return $dataTable->make();
  }
}