Laravel 5.4 如果用户是管理员,如何获取所有关系

Laravel 5.4 How to get all relations if user is an admin

$user->relatedModel(不带括号)return如果用户是管理员,则为整个 RelatedModel 集合;如果不是,则为典型的过滤集合。

我正在尝试改变一些用户关系,这样如果登录用户是管理员,那么他们就可以看到该关系中的所有模型,而不必在我每次需要使用它时都编写相同的条件,并且没有为我需要的每个相关模型的每个管理员在 property_user table 中创建所有关系。

这里有一个答案很清楚地解释了我想要实现的目标 这正是我正在寻找的想法,除了当我在 Laravel 5.4 中尝试此操作时,当用户是管理员时它会抛出异常。

Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

如果用户是标准用户,则关系 return 是适当的过滤集合。

这是该答案的代码示例

public function properties()
    {
    if ($this->isAdmin()) {
        return Property::query();
    } elseif ($this->isManager() || $this->isBroker()) {
        return $this->belongsToMany('App\Property');
    }

    return null;
}

这已经是一个已经编写了许多关系调用的大型站点,所以当我相信我可以简单地修改关系定义时,我不希望重构所有这些代码实例。

我如何 return 当用户是管理员时包含所有模型的此函数的关系,并保持它以便不需要修改站点周围 $user->properties 的使用?

[2018-06-02 12:58:23] local.ERROR: LogicException: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation in /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:403
Stack trace:
#0 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(386): Illuminate\Database\Eloquent\Model->getRelationshipFromMethod('sites')
#1 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(316): Illuminate\Database\Eloquent\Model->getRelationValue('sites')
#2 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1279): Illuminate\Database\Eloquent\Model->getAttribute('sites')
#3 /home/vagrant/Leadgen/app/Http/Controllers/Backend/SiteController.php(29): Illuminate\Database\Eloquent\Model->__get('sites')
#4 [internal function]: App\Http\Controllers\Backend\SiteController->index(Object(Illuminate\Http\Request))
#5 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(55): call_user_func_array(Array, Array)
#6 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(44): Illuminate\Routing\Controller->callAction('index', Array)
#7 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Route.php(203): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\Backend\SiteController), 'index')
#8 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Route.php(160): Illuminate\Routing\Route->runController()
#9 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Router.php(572): Illuminate\Routing\Route->run()
#10 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#11 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#12 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#13 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#14 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#15 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Auth\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure))
#16 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#17 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(65): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#18 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#19 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#20 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#21 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#22 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#23 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#24 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#25 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#26 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#27 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#28 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#29 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#30 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#31 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#32 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#33 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Router.php(574): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#34 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Router.php(533): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#35 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Router.php(511): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#36 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#37 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#38 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#39 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#40 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#41 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#42 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#43 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#44 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#45 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#46 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#47 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#48 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#49 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#50 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#51 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#52 /home/vagrant/Leadgen/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#53 /home/vagrant/Leadgen/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate

由于这不是真正的关系,因此您不能将其作为一个人访问。使用这个:

$sites = $user->sites()->get();

如果您想以 属性 的形式访问它并且不需要查询对象(例如添加其他约束),您可以使用访问器:

public function getPropertiesAttribute()
{
    if ($this->isAdmin()) {
        return Property::all();
    } elseif ($this->isManager() || $this->isBroker()) {
        return $this->belongsToMany('App\Property')->get();
    }

    return null;
}

一个肮脏的黑客:

public function sites()
{
    if ($this->isAdmin()) {
        return $this->hasMany('App\Site', 'id')->orWhereRaw(1);
    }
    return $this->belongsToMany('App\Site');
}