where(function ($q) use ($user) { $q->whereHas('roles', function ($q) use ($user) { $q->where('role_id', $user->userRole->role->id ?? "") ->where(function ($q) { $q->where('view', 1) ->orWhere('create', 1); }); })->doesntHave('childs'); })->orWhere(function ($q) use ($user) { $q->whereHas('childWithoutRecursion', function ($q) use ($user) { return $q->whereHas('roles', function ($q) use ($user) { $q->where('role_id', $user->userRole->role->id ?? "") ->where(function ($q) { $q->where('view', 1) ->orWhere('create', 1); }); }); }); }); } public function childsByAuthRoleId() { return $this->hasMany($this, "parent_id") ->with('childsByAuthRoleId') ->byAuthRoleId() ->orderBy('sequence', 'asc'); } public function childWithoutRecursion() { return $this->hasMany($this, 'parent_id'); } public function childs() { return $this->hasMany($this, "parent_id") ->with("childs") ->orderBy('sequence', 'asc'); } public function scopeAuth($q) { return $q->where('parent_id', null)->with("childs")->get(); } public function getNameAttribute() { return __("menu.{$this->menu_translate_key}"); } public function roles() { return $this->belongsToMany(Role::class, 'role_accesses') ->withPivot('view', 'create', 'update', 'delete'); } }