yajra/laravel-datatables-oracle
Laravel package for DataTables server-side processing via AJAX. Build JSON responses from Eloquent, Query Builder, or Collections using a fluent API (DataTables::eloquent/query/collection/make). Supports modern Laravel versions and common DataTables features.
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require yajra/laravel-datatables-oracle:"^12"
For Laravel 13, use yajra/laravel-datatables:"^13".
Publish Config (Optional):
php artisan vendor:publish --provider="Yajra\DataTables\DataTablesServiceProvider"
First Use Case: Create a route and controller method to return a DataTable response:
use Yajra\DataTables\Facades\DataTables;
Route::get('/users', function () {
return DataTables::eloquent(User::query())->toJson();
});
Frontend Integration: Include DataTables CSS/JS in your Blade view:
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
Initialize the table:
$(document).ready(function() {
$('#users-table').DataTable({
processing: true,
serverSide: true,
ajax: '/users'
});
});
Eloquent Integration:
// Basic usage
return DataTables::eloquent(User::query())
->addColumn('full_name', function($user) {
return $user->first_name . ' ' . $user->last_name;
})
->toJson();
// With custom queries
$query = User::where('active', 1);
return DataTables::eloquent($query)
->editColumn('status', function($user) {
return $user->active ? 'Active' : 'Inactive';
})
->toJson();
Query Builder Integration:
return DataTables::query(DB::table('users'))
->addColumn('full_name', function($user) {
return $user->first_name . ' ' . $user->last_name;
})
->toJson();
Collection Integration:
return DataTables::collection(User::all())
->addColumn('full_name', function($user) {
return $user->first_name . ' ' . $user->last_name;
})
->toJson();
Server-Side Processing with Oracle:
return DataTables::eloquent(User::query())
->setConnection('oracle') // Explicitly set Oracle connection
->addIndexColumn()
->addColumn('action', function($user) {
return '<button class="edit-btn" data-id="' . $user->id . '">Edit</button>';
})
->rawColumns(['action'])
->make(true);
Custom Filtering:
return DataTables::eloquent(User::query())
->filterColumn('status', function($query, $keyword) {
$query->where('active', 'like', "%{$keyword}%");
})
->toJson();
Dynamic Columns:
$columns = [
['data' => 'id', 'name' => 'id', 'title' => 'ID'],
['data' => 'name', 'name' => 'name', 'title' => 'Name'],
];
return DataTables::eloquent(User::query())
->setColumns($columns)
->toJson();
API Resource Integration:
return DataTables::resource(UserResource::collection(User::all()))
->addColumn('custom_field', function($resource) {
return $resource->custom_field;
})
->toJson();
Blade Integration:
<table id="users-table" class="display">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
</table>
@push('scripts')
<script>
$(function() {
$('#users-table').DataTable({
processing: true,
serverSide: true,
ajax: '{!! route('users.datatable') !!}',
columns: [
{ data: 'id', name: 'id' },
{ data: 'name', name: 'name' },
{ data: 'action', name: 'action', orderable: false, searchable: false }
]
});
});
</script>
@endpush
Laravel Mix/Webpack:
Ensure DataTables JS/CSS are included in your resources/js/app.js or resources/css/app.css.
API Routes:
Route::apiResource('users', UserController::class)->only(['index']);
Route::get('users/datatable', [UserController::class, 'datatable'])->name('users.datatable');
Oracle-Specific Issues:
return DataTables::eloquent(User::query())
->selectRaw('id as user_id, name as user_name')
->toJson();
return DataTables::eloquent(User::query())
->selectRaw('"FirstName" as first_name')
->toJson();
Debugging:
APP_DEBUG=true to log queries and inputs.->toArray() to inspect the raw response before converting to JSON:
return DataTables::eloquent(User::query())->toArray();
Performance:
select('*'): Explicitly define columns to reduce query load.->make(true): For large datasets, enable server-side processing explicitly:
return DataTables::eloquent(User::query())->make(true);
orderBy and where clauses.Configuration Quirks:
order, group) as column names.'column_defs' => [
'orderable' => false,
'searchable' => false,
'visible' => false,
],
Log Queries:
DB::enableQueryLog();
$result = DataTables::eloquent(User::query())->toJson();
\Log::info('Queries:', ['queries' => DB::getQueryLog()]);
Inspect Request:
\Log::info('DataTables Request:', request()->all());
Test with Postman: Send a POST request to your DataTable endpoint with the following body:
{
"draw": 1,
"columns": [
{ "data": "id", "name": "id", "searchable": true, "orderable": true, "search": { "value": "", "regex": false } },
{ "data": "name", "name": "name", "searchable": true, "orderable": true, "search": { "value": "", "regex": false } }
],
"order": [
{ "column": 0, "dir": "asc" }
],
"start": 0,
"length": 10,
"search": { "value": "", "regex": false }
}
Custom Request Handling: Override the request handling in your controller:
public function datatable()
{
$request = DataTables::of(User::query())->getRequest();
// Custom logic here
return DataTables::of(User::query())->make(true);
}
Middleware for DataTables:
Route::middleware(['datatables'])->group(function () {
Route::get('/admin/users', [UserController::class, 'datatable']);
});
Event Listeners:
Listen to DataTables events (e.g., datatables.pre-query):
DataTables::preQuery(function ($query) {
// Modify the query before execution
});
Service Provider Extensions: Bind custom DataTable classes in your service provider:
$this->app->bind('datatables.custom', function () {
return new CustomDataTable();
});
How can I help you explore Laravel packages today?