Author - StudySection Post Views - 61 views
Laravel | json

Laravel camelcase json Response

You all know that the naming convention for database fields should be in the snake_case format. Hence, fetching the data from the database will also result in the snake_case format. So, if you want to get data in camelCase format for your json response, Laravel provides a library called grohiro/laravel-camelcase-json.By using this library, you don’t have to make any changes to your model class or any database queries. Steps to use this library are given below:

Installation:

$ composer require 'grohiro/laravel-camelcase-json'
Add the service provider.
config/app.php
provider' => [
// default providers
// ...
Grohiro\LaravelCamelCaseJson\CamelCaseJsonResponseServiceProvider::class,
],

Usage:
In your controller class:
return response()->json($model);
// => ['userName' => 'foo', 'userKey' => 'bar', ...] All the database fields which are in snake case format will result as camelcase.

StudySection has a long list of certification exams that it offers through its online platform. The PHP Certification Exam is one of the programming certifications that it provides. Whether you are new to PHP programming or you have extensive experience in PHP programming, you can get a certification according to your level. Attach a PHP certification with your resume to get the most out of job offers.

Leave a Reply

Your email address will not be published.