Hide Attributes from JSON
When you are working with Eloquent Object, you can apply the makeHidden() (Laravel documentation) function before converting your object toArray().
This can prevent overriding attributes to be computed and increase the performance of converting the object into an array.
Basic Usage
use Yajra\DataTables\Facades\DataTables;use App\Models\User; Route::get('user-data', function() { $model = User::with('posts.comment')->query(); return DataTables::eloquent($model) ->makeHidden('posts') ->toJson();});
Common Use Cases
Hide Sensitive Data
return DataTables::eloquent(User::query()) ->makeHidden(['password', 'api_token', 'remember_token']) ->toJson();
Hide Large Relationships
return DataTables::eloquent(Post::query()) ->makeHidden(['comments', 'metadata']) ->toJson();
Combine with Make Visible
return DataTables::eloquent(User::query()) ->makeHidden('posts') // Hide from serialization ->makeVisible('secret_field') // Force visible ->toJson();
Performance Benefits
| Method | Performance | Use Case |
|---|---|---|
makeHidden |
✅ Faster | Prevent attribute computation |
makeHidden |
✅ Better | Large relationships |
[!TIP] Using
makeHiddenat the model level is more performant than filtering columns after serialization.
See Also
- Remove Column - Remove columns from response
- Raw Columns - Allow HTML rendering