![]()
WARNING You're browsing the documentation for an upcoming version of Laravel DataTables. The documentation and features of this release are subject to change.
Enable Eager Join Aliases
The enableEagerJoinAliases() API enables the generation of unique table aliases when joining eagerly loaded relationships. This solves the "Not unique table/alias" error when performing searches or ordering on columns from relationships.
Basic Usage
use Yajra\DataTables\Facades\DataTables;use App\Models\Post; Route::get('post-data', function() { return DataTables::eloquent( Post::with(['author', 'comments', 'tags']) ) ->enableEagerJoinAliases() ->toJson();});
The Problem
When you have nested eager-loaded relationships and try to search/sort on relationship columns, you may encounter:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'users'
This happens because multiple joins reference the same table without unique aliases.
The Solution
enableEagerJoinAliases() generates unique aliases for each joined table:
// Without aliases - causes conflictsusersusers AS users_authorusers AS users_comments_pivottags AS tags_posts_pivotposts AS posts_author
Supported Relations
This feature works with:
HasOneHasManyBelongsToBelongsToManyHasOneThrough
Example with Multiple Relations
use Yajra\DataTables\Facades\DataTables;use App\Models\User; Route::get('user-data', function() { // Eager load all relationships $model = User::with([ 'posts.comments', // Nested: posts -> comments 'roles', // Direct: roles 'profile', // HasOne: profile 'teams.members', // BelongsToMany: teams -> members ]); return DataTables::eloquent($model) ->enableEagerJoinAliases() ->toJson();});
See Also
- Search Eloquent Relationships - Basic relationship searching
- Order Column - Custom column ordering