Php Laravel是否自动将集合返回为键/值?

Php Laravel是否自动将集合返回为键/值?,php,laravel,laravel-5,Php,Laravel,Laravel 5,在我的应用程序中,我在一个表中有一个键/值权限对,该表被设置为对我的用户的hasMany: public function permissions() { return $this->hasMany('Permissions'); } 与只包含数组的集合不同,我希望它作为键值数组返回,这样我就可以访问以下权限: $user->permissions->blah; 我尝试过做一些后期处理,但当我直接修改了权限属性或创建了一个新的perms属性时,它将其视为一个修改过的

在我的应用程序中,我在一个表中有一个键/值权限对,该表被设置为对我的用户的
hasMany

public function permissions()
{
    return $this->hasMany('Permissions');
}
与只包含数组的集合不同,我希望它作为键值数组返回,这样我就可以访问以下权限:

$user->permissions->blah;
我尝试过做一些后期处理,但当我直接修改了
权限
属性或创建了一个新的
perms
属性时,它将其视为一个修改过的属性,这会弄乱我可能需要执行的任何实际保存


有没有一种方法可以在运行中实现这一点,或者直接在
权限
用户
模型中设置一些钩子?

此函数用于创建有说服力的关系


如果您希望获得自定义响应,则需要创建新方法,该方法返回的值与您希望看到的值完全相同。

此函数用于创建有说服力的关系


如果您希望获得自定义响应,则需要创建新方法,该方法返回的值与您希望看到的值完全相同。

此函数用于创建有说服力的关系


如果您希望获得自定义响应,则需要创建新方法,该方法返回的值与您希望看到的值完全相同。

此函数用于创建有说服力的关系

如果您想获得自定义响应,则需要创建新方法,该方法返回的值与您希望看到的值完全相同。

是正确的

照亮\数据库\雄辩\模型

  /**
         * Dynamically retrieve attributes on the model.
         *
         * @param  string  $key
         * @return mixed
         */
        public function __get($key)
        {
            return $this->getAttribute($key);
        }


public function getAttribute($key)
    {
        $inAttributes = array_key_exists($key, $this->attributes);

        // If the key references an attribute, we can just go ahead and return the
        // plain attribute value from the model. This allows every attribute to
        // be dynamically accessed through the _get method without accessors.
        if ($inAttributes || $this->hasGetMutator($key))
        {
            return $this->getAttributeValue($key);
        }

        // If the key already exists in the relationships array, it just means the
        // relationship has already been loaded, so we'll just return it out of
        // here because there is no need to query within the relations twice.
        if (array_key_exists($key, $this->relations))
        {
            return $this->relations[$key];
        }

        // If the "attribute" exists as a method on the model, we will just assume
        // it is a relationship and will load and return results from the query
        // and hydrate the relationship's value on the "relationships" array.
        if (method_exists($this, $key))
        {
            return $this->getRelationshipFromMethod($key);
        }
    }
实际上

$user->permissions等于$user->permissions()->get()

如果要进行后期处理,请创建一个新函数,如:

public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = \Hash::make($value);
    }

是的,没错

照亮\数据库\雄辩\模型

  /**
         * Dynamically retrieve attributes on the model.
         *
         * @param  string  $key
         * @return mixed
         */
        public function __get($key)
        {
            return $this->getAttribute($key);
        }


public function getAttribute($key)
    {
        $inAttributes = array_key_exists($key, $this->attributes);

        // If the key references an attribute, we can just go ahead and return the
        // plain attribute value from the model. This allows every attribute to
        // be dynamically accessed through the _get method without accessors.
        if ($inAttributes || $this->hasGetMutator($key))
        {
            return $this->getAttributeValue($key);
        }

        // If the key already exists in the relationships array, it just means the
        // relationship has already been loaded, so we'll just return it out of
        // here because there is no need to query within the relations twice.
        if (array_key_exists($key, $this->relations))
        {
            return $this->relations[$key];
        }

        // If the "attribute" exists as a method on the model, we will just assume
        // it is a relationship and will load and return results from the query
        // and hydrate the relationship's value on the "relationships" array.
        if (method_exists($this, $key))
        {
            return $this->getRelationshipFromMethod($key);
        }
    }
实际上

$user->permissions等于$user->permissions()->get()

如果要进行后期处理,请创建一个新函数,如:

public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = \Hash::make($value);
    }

是的,没错

照亮\数据库\雄辩\模型

  /**
         * Dynamically retrieve attributes on the model.
         *
         * @param  string  $key
         * @return mixed
         */
        public function __get($key)
        {
            return $this->getAttribute($key);
        }


public function getAttribute($key)
    {
        $inAttributes = array_key_exists($key, $this->attributes);

        // If the key references an attribute, we can just go ahead and return the
        // plain attribute value from the model. This allows every attribute to
        // be dynamically accessed through the _get method without accessors.
        if ($inAttributes || $this->hasGetMutator($key))
        {
            return $this->getAttributeValue($key);
        }

        // If the key already exists in the relationships array, it just means the
        // relationship has already been loaded, so we'll just return it out of
        // here because there is no need to query within the relations twice.
        if (array_key_exists($key, $this->relations))
        {
            return $this->relations[$key];
        }

        // If the "attribute" exists as a method on the model, we will just assume
        // it is a relationship and will load and return results from the query
        // and hydrate the relationship's value on the "relationships" array.
        if (method_exists($this, $key))
        {
            return $this->getRelationshipFromMethod($key);
        }
    }
实际上

$user->permissions等于$user->permissions()->get()

如果要进行后期处理,请创建一个新函数,如:

public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = \Hash::make($value);
    }

是的,没错

照亮\数据库\雄辩\模型

  /**
         * Dynamically retrieve attributes on the model.
         *
         * @param  string  $key
         * @return mixed
         */
        public function __get($key)
        {
            return $this->getAttribute($key);
        }


public function getAttribute($key)
    {
        $inAttributes = array_key_exists($key, $this->attributes);

        // If the key references an attribute, we can just go ahead and return the
        // plain attribute value from the model. This allows every attribute to
        // be dynamically accessed through the _get method without accessors.
        if ($inAttributes || $this->hasGetMutator($key))
        {
            return $this->getAttributeValue($key);
        }

        // If the key already exists in the relationships array, it just means the
        // relationship has already been loaded, so we'll just return it out of
        // here because there is no need to query within the relations twice.
        if (array_key_exists($key, $this->relations))
        {
            return $this->relations[$key];
        }

        // If the "attribute" exists as a method on the model, we will just assume
        // it is a relationship and will load and return results from the query
        // and hydrate the relationship's value on the "relationships" array.
        if (method_exists($this, $key))
        {
            return $this->getRelationshipFromMethod($key);
        }
    }
实际上

$user->permissions等于$user->permissions()->get()

如果要进行后期处理,请创建一个新函数,如:

public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = \Hash::make($value);
    }


属性访问器

我会这样做的。首先在模型上创建一个受保护/私有数据成员,以保存键/值对。这将阻止Eloquent尝试将此保存为模型属性

protected $permValues = [];
接下来创建一个键/值数组,这样我们就可以在它还不存在的情况下构建它们

public getPermissionValuesAttribute()
{
    if (empty($this->permValues))
    {
        $this->permValues = $this->permissions->lists('value', 'key');
    }

    return $this->permValues;
}
这使事情几乎像你希望的那样

$model->permissionValues['blah'];
神奇方法

您可以通过创建一个类来保存权限集并定义魔术方法来进一步实现这一点

protected $permSet;

public getPermissionSetAttribute()
{
    if (empty($this->permSet))
    {
        $this->permSet = new PermissionSet($this->permissions);
    }

    return $this->permSet;
}
我将让您填写PermissionSet类的空白,但最终结果将完全符合您的要求

$model->permissionSet->blah;

属性访问器

我会这样做的。首先在模型上创建一个受保护/私有数据成员,以保存键/值对。这将阻止Eloquent尝试将此保存为模型属性

protected $permValues = [];
接下来创建一个键/值数组,这样我们就可以在它还不存在的情况下构建它们

public getPermissionValuesAttribute()
{
    if (empty($this->permValues))
    {
        $this->permValues = $this->permissions->lists('value', 'key');
    }

    return $this->permValues;
}
这使事情几乎像你希望的那样

$model->permissionValues['blah'];
神奇方法

您可以通过创建一个类来保存权限集并定义魔术方法来进一步实现这一点

protected $permSet;

public getPermissionSetAttribute()
{
    if (empty($this->permSet))
    {
        $this->permSet = new PermissionSet($this->permissions);
    }

    return $this->permSet;
}
我将让您填写PermissionSet类的空白,但最终结果将完全符合您的要求

$model->permissionSet->blah;

属性访问器

我会这样做的。首先在模型上创建一个受保护/私有数据成员,以保存键/值对。这将阻止Eloquent尝试将此保存为模型属性

protected $permValues = [];
接下来创建一个键/值数组,这样我们就可以在它还不存在的情况下构建它们

public getPermissionValuesAttribute()
{
    if (empty($this->permValues))
    {
        $this->permValues = $this->permissions->lists('value', 'key');
    }

    return $this->permValues;
}
这使事情几乎像你希望的那样

$model->permissionValues['blah'];
神奇方法

您可以通过创建一个类来保存权限集并定义魔术方法来进一步实现这一点

protected $permSet;

public getPermissionSetAttribute()
{
    if (empty($this->permSet))
    {
        $this->permSet = new PermissionSet($this->permissions);
    }

    return $this->permSet;
}
我将让您填写PermissionSet类的空白,但最终结果将完全符合您的要求

$model->permissionSet->blah;

属性访问器

我会这样做的。首先在模型上创建一个受保护/私有数据成员,以保存键/值对。这将阻止Eloquent尝试将此保存为模型属性

protected $permValues = [];
接下来创建一个键/值数组,这样我们就可以在它还不存在的情况下构建它们

public getPermissionValuesAttribute()
{
    if (empty($this->permValues))
    {
        $this->permValues = $this->permissions->lists('value', 'key');
    }

    return $this->permValues;
}
这使事情几乎像你希望的那样

$model->permissionValues['blah'];
神奇方法

您可以通过创建一个类来保存权限集并定义魔术方法来进一步实现这一点

protected $permSet;

public getPermissionSetAttribute()
{
    if (empty($this->permSet))
    {
        $this->permSet = new PermissionSet($this->permissions);
    }

    return $this->permSet;
}
我将让您填写PermissionSet类的空白,但最终结果将完全符合您的要求

$model->permissionSet->blah;

尼斯照明\数据库\雄辩\模型@tekNice照明\数据库\雄辩\模型@tekNice照明\数据库\雄辩\模型@tekNice照明\数据库\雄辩\模型@tekNice照明\数据库\雄辩\模型@tek