grohiro / laravel-camelcase-json Goto Github PK
View Code? Open in Web Editor NEWConvert response JSON key to camelCase
License: MIT License
Convert response JSON key to camelCase
License: MIT License
Since global string and array helpers are deprecated (Laravel news) this package isn't working anymore with newest versions of Laravel.
Proposed solution
use Illuminate\Support\Str;
public function encodeArray($array)
{
$newArray = [];
foreach ($array as $key => $val) {
// Deprecated
// $newArray[\camel_case($key)] = $this->encodeJson($val);
// Use this directly
$newArray[Str::camel($key)] = $this->encodeJson($val);
}
return $newArray;
}
Another workaround since pull request is still open:
app/Http/Helpers.php
use Illuminate\Support\Str;
if (!function_exists('camel_case')) {
function camel_case($input)
{
return Str::camel($input);
}
}
Hi there,
Really love this package. However I am facing an issue where there is a condition that force me to use Laravel's DB facade instead of Eloquent Model. Please refer to following code:
$users = DB::table('users')->select('name AS some_cool_name')->get()
return response()->json($users);
The result shows "some_cool_name" instead of "someCoolName"
I am using Laravel 5.4
This is the error i get sometimes
[Sun Apr 28 15:41:49 2019] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in C:\Projects\Hospital\Prototype\laravel-hospital-man-sys\vendor\grohiro\laravel-camelcase-json\src\CamelCaseJsonResponseFactory.php on line 33
[Sun Apr 28 15:41:49 2019] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0
This module craches artisan if you use session driver redis.
If you change public function register() to public function boot() in CamelCaseJsonResponseServiceProvider.php, the bug is fixed.
How I can use your package? After install the JSON the same as Laravel original.
What I need to do to activate your package?
P.S. I use Laravel 5.7
response()->json($model)
returns the original json
{
"created_at": "..."
"updated_at": "..."
}
I find that using this library generate a conflict with Api Resources, causing the ToArray not to be called.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.