如何更改Laravel中属性的名称

如何更改Laravel中属性的名称,laravel,Laravel,我有一个模型名为: Post with attributes id | name | extra_income 当我从请求中获取数据时,extra\u income将作为extra\u pay 所以当我做Post::create(request()->all())额外收入为空,原因很明显,名称不匹配 问题是有没有办法修改Post模型上的extra\u income属性,这样我就可以将其映射到extra\u pay,因为我知道您在表单中有extra\u pay,在posts表中有extra\

我有一个模型名为:

Post  with attributes id | name | extra_income 
当我从请求中获取数据时,
extra\u income
将作为
extra\u pay
所以当我做
Post::create(request()->all())
额外收入
为空,原因很明显,名称不匹配


问题是有没有办法修改Post模型上的
extra\u income
属性,这样我就可以将其映射到
extra\u pay
,因为我知道您在表单中有
extra\u pay
,在
posts
表中有
extra\u income
。处理此问题的最佳方法是将表单中元素的名称更改为
extra\u income

或者,您可以这样做:

$data = request()->all();
$data['extra_income'] = $data['extra_pay'];
Post::create($data);
或:


据我所知,您在表格中有
extra\u pay
,在
posts
表格中有
extra\u income
。处理此问题的最佳方法是将表单中元素的名称更改为
extra\u income

或者,您可以这样做:

$data = request()->all();
$data['extra_income'] = $data['extra_pay'];
Post::create($data);
或:

你在找我。而且不要忘记将其附加到
$filleble
属性中,因为您将要使用
create

class Post
{
    protected $fillable = [
        // ...
        'extra_pay',
    ];

    public function setExtraPayAttribute($value)
    {
        $this->attribute['extra_income'] = $value;
    }

}
你在找我。而且不要忘记将其附加到
$filleble
属性中,因为您将要使用
create

class Post
{
    protected $fillable = [
        // ...
        'extra_pay',
    ];

    public function setExtraPayAttribute($value)
    {
        $this->attribute['extra_income'] = $value;
    }

}

你可以在3个地方做

  • 改变你的状态,这是最好的选择
  • 在传入请求中使用merge
    $request->merge(['extra_income'=>$request->extra_pay])更改它
  • 或者在Post::create方法中通过手动设置所有值来更改它

您可以在3个位置执行此操作

  • 改变你的状态,这是最好的选择
  • 在传入请求中使用merge
    $request->merge(['extra_income'=>$request->extra_pay])更改它
  • 或者在Post::create方法中通过手动设置所有值来更改它

感谢您抽出时间,但我已经知道了解决方案!问题是我确实有多个属性从前面映射到不同的名称!我只是简化了我的问题。所以我认为应该有一个更好的方法来做这件事,但我已经知道了解决办法!问题是我确实有多个属性从前面映射到不同的名称!我只是简化了我的问题。所以我认为应该有一个更好的方法,你在玩哪个版本?Laravel 5.6.0@Chay22何时发布Laravel 5.6@HirenGohel它将于2018年1月发布,但我的案例有点科学性;)你在玩哪个版本?Laravel 5.6.0@Chay22何时发布Laravel 5.6@HirenGohel它将于2018年1月发布,但我的案例有点科学性;)