Skip to content
Advertisement

Laravel controller store data received in camel case format

I’m working with multiple controllers where I receive inputs in camel case format and I need to assign these properties to a model:

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

The problem is that my model has around 30 properties and I don’t want to write them one by one. I know that I can use the request to get all the properties:

$input = $request->all();

Is there a way to convert match the properties in camel case format with the database standard equivalent? Something like the inverse of camel method:

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

Advertisement

Answer

Use

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

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

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement