PHP code example of ributwiboworahayu / datatable-service
1. Go to this page and download the library: Download ributwiboworahayu/datatable-service 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/ */
ributwiboworahayu / datatable-service example snippets
namespace App\Http\Controllers;
use DatatableService\DataTableService; // Import the class
use Illuminate\Http\Request;
class ExampleController extends Controller
{
protected $dataTableService;
public function __construct(DataTableService $dataTableService) // Dependency injection
{
$this->dataTableService = $dataTableService;
}
public function index(Request $request)
{
$query = YourModel::query(); // Replace with your model
$columns = ['tableName.column1', 'tableName.column2']; // Replace with relevant columns
// Using applyDataTables to get DataTables response
$dataTablesResponse = $this->dataTableService->applyDataTables($query, $request, $columns);
if(!$dataTablesResponse['ajax']) return redirect()->route('dashboard.index')->withErrors('Invalid request'); // redirect if needed
return response()->json($dataTablesResponse);
}
}
use App\Http\Controllers\ExampleController;
Route::get('/data', [ExampleController::class, 'index']);
namespace App\Http\Controllers;
use DatatableService\DataTableService; // Mengimpor kelas
use Illuminate\Http\Request;
class ExampleController extends Controller
{
protected $dataTableService;
public function __construct(DataTableService $dataTableService) // Dependency injection
{
$this->dataTableService = $dataTableService;
}
public function index(Request $request)
{
$query = YourModel::query(); // Ganti dengan model Anda
$columns = ['tableName.column1', 'tableName.column2']; // Ganti dengan kolom yang relevan
// Menggunakan applyDataTables untuk mendapatkan response DataTables
$dataTablesResponse = $this->dataTableService->applyDataTables($query, $request, $columns);
if(!$dataTablesResponse['ajax']) return redirect()->route('dashboard.index')->withErrors('Invalid request'); // redirect if needed
return response()->json($dataTablesResponse);
}
}
use App\Http\Controllers\ExampleController;
Route::get('/data', [ExampleController::class, 'index']);