【问题标题】:Laravel controller store data received in camel case formatLaravel 控制器存储以驼峰格式接收的数据
【发布时间】:2023-03-22 05:03:01
【问题描述】:

我正在使用多个控制器,在这些控制器中我以驼峰格式接收输入,我需要将这些属性分配给模型:

$scopeCommercial = new ScopeCommercial();
$scopeCommercial->lifetime_sales = $request->lifetimeSales;
$scopeCommercial->lifetime_volumes = $request->lifetimeVolumes;

问题是我的模型有大约 30 个属性,我不想一一写出来。 我知道我可以使用request 来获取所有属性:

$input = $request->all();

有没有办法将驼峰格式的属性与数据库标准等效转换?类似于 camel 方法的逆:

$converted = Str::camel('foo_bar');

【问题讨论】:

    标签: php laravel database api


    【解决方案1】:

    使用

    $converted = Str::snake('fooBar', '_');
    

    更多https://laravel.com/docs/8.x/helpers#method-snake-case

    【讨论】:

      猜你喜欢
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 2014-05-16
      相关资源
      最近更新 更多