Skip to content

Commit

Permalink
Prevent Lazy Load
Browse files Browse the repository at this point in the history
  • Loading branch information
SupianIDz committed Dec 30, 2021
1 parent 7279413 commit 45d7024
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Http/Controllers/ImpersonateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class ImpersonateController extends Controller
*/
protected Impersonate $impersonate;

/**
* @var array|string[]
*/
protected array $roleProviders = [
'Spatie\Permission\Traits\HasRoles', // spatie/laravel-permission
'Laratrust\Traits\LaratrustUserTrait' // santigarcor/laratrust
];

/**
* ImpersonateController constructor.
* @param Impersonate $impersonate
Expand All @@ -36,7 +44,18 @@ public function __construct(Impersonate $impersonate)
*/
public function list(Request $request) : Collection
{
$query = App::make(config('impersonate.model'))->query();
$query = App::make(
$model = config('impersonate.model')
)->query();

// Prevent lazy loading roles
$classUses = class_uses($model);
foreach ($this->roleProviders as $provider) {
if (isset($classUses[$provider])) {
$query->with('roles');
break;
}
}

$query->when($request->has('search'), function ($query) use ($request) {
$query->where(function ($query) use ($request) {
Expand Down

0 comments on commit 45d7024

Please sign in to comment.