1. Go to this page and download the library: Download livecms/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/ */
livecms / datatables example snippets
use App\Http\Resources\SportResource;
use App\Sport;
use LiveCMS\DataTables\HasDataTables;
class SportDataTables implements HasDataTables
{
public function toDataTablesQuery()
{
// use one of these types
return app(Sport::class)->newQuery(); // Builder
return app(Sport::class)->players(); // Relation
return Sport::get(); // Collection
return SportResource::collection(Sport::get()); // API Resource Collection
}
}
use Illuminate\Support\HtmlString;
use LiveCMS\DataTables\HasDataTables;
class SportDataTables implements HasDataTables
{
....
public function getDataTablesFields()
{
return [
'ID',
'Sport Name' => 'name',
'Is Active' => [
'display' => function ($isActive) {
return new HtmlString(
$isActive ? '<strong>True</strong>' : '<strong>False</strong>';
);
}
],
'Action' => function ($row) {
return new HtmlString(
'<button data-id="'.$row->id.'">Edit</button><button data-id="'.$row->id.'">Delete</button>';
);
}
];
}
}
'Label' => [
'name' => 'label', // field name or will use lower case of label if not defined
'data' => 'label', // data name or will use lower case of label if not defined
'orderable' => true, // if this field is not orderable, set false,
'searchable' => true, // if this field is not searchable, set false
'display' => function ($value) {
return new \Illuminate\Support\HtmlString('<span class="rounded">value</span>');
,
// if you want to mark up the value, use display.
],
'Label' => function ($row) {
return 'anything';
},
use LiveCMS\DataTables\DataTables;
class SportController extends Controller
{
protected $dataTables;
public function __construct(SportDataTables $sportDataTables)
{
$dataTablesUrl = url('/admin/sport/data'); // route('routename')
$this->dataTables = new DataTables($sportDataTables, $dataTablesUrl);
}
public function getIndex()
{
$this->dataTables->renderView();
return view('admin.sports.index');
}
public function postData(Request $request)
{
return $this->dataTables->renderData();
}
}