Star

Laravel

Use the Eloquent Builder directly

You can pass an Eloquent Builder instance straight to the library — no need to write raw SQL.
The example below is identical to Join Tables, written with Eloquent.
IDTrack NameAlbumMediaTypeUnitPriceMillisecondsBytes
Loading…
<?php
use App\Models\Track;
use Ozdemir\Datatables\Datatables;
use Ozdemir\Datatables\DB\LaravelAdapter;

Route::get('/ajax/laravel', function () {
    $sql = Track::select([
        'TrackId',
        'Track.Name',
        'Title as Album',
        'MediaType.Name as MediaType',
        'UnitPrice',
        'Milliseconds',
        'Bytes',
    ])
        ->join('Album', 'Album.AlbumId', 'Track.AlbumId')
        ->join('MediaType', 'MediaType.MediaTypeId', 'Track.MediaTypeId');

    $dt = new Datatables(new LaravelAdapter);
    $dt->query($sql);

    return $dt->generate();
});