Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5全局范围:注入变量_Laravel_Scope_Laravel Eloquent - Fatal编程技术网

Laravel 5全局范围:注入变量

Laravel 5全局范围:注入变量,laravel,scope,laravel-eloquent,Laravel,Scope,Laravel Eloquent,比如说,我有5个系统(让我们这样称呼它们) 每个系统都有员工 当system#1的员工登录时,我只想向他显示为system#1工作的其他员工的列表 我想使用全局范围 但是我找不到一种方法来注入登录用户的系统id 我尝试了匿名全局作用域-但静态启动无法接受系统\u id: protected static function boot() { //this will not work, as scope method is static $userId = a

比如说,我有5个系统(让我们这样称呼它们)

每个系统都有员工

当system#1的员工登录时,我只想向他显示为system#1工作的其他员工的列表

我想使用全局范围

但是我找不到一种方法来注入登录用户的系统id

我尝试了匿名全局作用域-但静态启动无法接受系统\u id:

protected static function boot() { //this will not work, as scope method is static $userId = auth()->user()->system_id; parent::boot(); static::addGlobalScope('thisSystemUser', function (Builder $builder) use($userId) { $builder->where('system_id', $userId); }); } 受保护的静态函数启动() { //这将不起作用,因为scope方法是静态的 $userId=auth()->user()->system\u id; 父::boot(); 静态::addGlobalScope('thisSystemUser',函数(Builder$Builder)use($userId){ $builder->where('system_id',$userId); }); } 我还尝试了使用单独类的全局作用域-同样的问题:

namespace App\Scopes; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Scope; class SystemEmployeeScope implements Scope { /** * Apply the scope to a given Eloquent query builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model * @return void */ public function apply(Builder $builder, Model $model) { // no luck here as well $userId = auth()->user()->system_id; /** * appended query constraint for this scope */ $builder->where('system_id', $userId); } } 名称空间应用程序\范围; 使用Illumb\Database\Elount\Builder; 使用Illumb\Database\Elount\Model; 使用Illumb\Database\Elount\Scope; 类SystemEmployeeScope实现范围 { /** *将范围应用于给定的雄辩的查询生成器。 * *@param\illumb\Database\elount\Builder$Builder *@param\illumb\Database\elount\Model$Model *@返回无效 */ 公共功能应用(Builder$Builder,Model$Model) { //这里也不走运 $userId=auth()->user()->system\u id; /** *此范围的附加查询约束 */ $builder->where('system_id',$userId); } } 我尝试使用$model,但没有效果。它返回空模型。任何通过$model获取数据的尝试都失败了

我不想使用局部作用域或动态作用域,因为它们不是我想要实现的,在我的案例中使用它们有更好的方法,例如:直接基于关系的条件-在我的案例中是多对多


有没有办法,我不知道,也不太复杂,无法将系统id塞进范围?

您可以这样做

namespace App\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class SystemEmployeeScope implements Scope
{

    private $user;

    public function __construct()
    {
        $this->user = auth()->user();
    }

    public function apply(Builder $builder, Model $model)
    {
        $userId = $this->user->id
        $builder->where('system_id', $userId);
    }
}

您使用的是哪个版本的Laravel?很抱歉迟到了-不同的时区和TGIF:)L5.6