
How to return data in json format in Laravel
How to return data in json format in Laravel
With this article, we’ll look at some examples of how to address the "How to return data in json format in Laravel" problem.
I got the user records using **`get()`** method from the users table and i want to return it in json format from controller.-
Return data in json format from controller file
Laravel syntax to convert data in json format
response()->json($data)
Example code
app\Http\Controllers\HomeController.php
$data = App\Models\User::get(); return response()->json($data);
Ouput
[{"id":1,"name":"Mr. Jay Price Jr.","email":"[email protected]","email_verified_at":"2022-03-03T11:01:39.000000Z","username":"","created_at":"2022-03-03T11:01:39.000000Z","updated_at":"2022-03-03T11:01:39.000000Z"}, {"id":2,"name":"Prof. Jarvis Lehner","email":"[email protected]","email_verified_at":"2022-03-03T11:01:39.000000Z","username":"", "created_at":"2022-03-03T11:01:39.000000Z","updated_at":"2022-03-03T11:01:39.000000Z"}]
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to run a specific seeder class in laravel
- How to fill a column automatically while creating records in Laravel
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- How to generate .env file for laravel?
- Always load the relationship data with eager loading in Laravel
- Call to undefined relationship [user] on model [App\Models\Post]
- How to get IP address in laravel
- Page loader in laravel
- Create model with migration and seeder
- Database transactions in laravel
- Call to undefined method Illuminate\Support\Facades\Request::all()
- How to create and run user seeder in laravel
- Rename Pivot Table in Laravel
- How to get query string value in laravel
- Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given
- How to get all route list
- Send OTP using textlocal api in laravel
- Create project table with model and migration
- Php artisan make model, factory, migration and controller in single command
- How to pass variable from controller to model in Laravel
- How to remove P tag from CkEditor in Laravel?
- How to get laravel errors folder in views directory in laravel
- How to display order by null last in laravel
- How to access the nth object from Laravel collection object ?
- How to add a key value pair to existing array in laravel