Skip to content
Icon

WARNING You're browsing the documentation for an upcoming version of Laravel DataTables. The documentation and features of this release are subject to change.

Search Eloquent Relationships

DataTables supports searching and sorting on eager loaded Eloquent relationships using dot notation.


Setup and Searching

Backend: Eager Load Relationships

Use Laravel's with() to eager load relationships:

Route::get('user-data', function () {
return DataTables::eloquent(User::with('posts'))
->addColumn('posts', fn (User $user) =>
$user->posts
->map(fn(Post $post) => Str::limit($post->title, 30, '...'))
->implode('<br>')
)
->toJson();
});

Frontend: Use Dot Notation for Searching

Use relation.column syntax in the name attribute:

$('#users-table').DataTable({
processing: true,
serverSide: true,
ajax: '/user-data',
columns: [
{ data: 'id', name: 'id' },
{ data: 'name', name: 'name' },
{ data: 'email', name: 'email' },
{ data: 'posts', name: 'posts.title' },
{ data: 'created_at', name: 'created_at' }
]
});

[!NOTE]

  • data: 'posts' - display key shown in the table
  • name: 'posts.title' - relationship.column for searching/sorting

Table Aliases

[!WARNING] Include select('table.*') in your query to prevent id column conflicts from related models.

Route::get('user-data', function () {
// Use table alias to prevent id conflicts
$model = Post::with('user')->select('posts.*');
 
return DataTables::eloquent($model)->toJson();
});

Nested Relationships

Search through deeply nested relationships:

Route::get('user-data', function () {
$model = User::with(['posts.comments', 'roles']);
 
return DataTables::eloquent($model)->toJson();
});

In JavaScript, reference nested columns with dot notation:

{ data: 'comments', name: 'posts.comments.content' }

[!WARNING] Nested relationship sorting may have limitations. Searching is fully supported.


See Also