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":"ziemann.eldora@example.net","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":"torey68@example.org","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
- Laravel create default admin user
- Laravel 11 step by step instructions to upload file in storage directory and display in blade file
- How to pass external link in laravel blade to anchor tag
- How to restore multiple records after soft-deletes in Laravel
- Class 'App\Providers\Auth' not found
- Laravel append URI in route
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- Where to use whereNotNull eloquent in laravel
- Link storage folder in laravel 8
- Get only 10 records from table in laravel
- How to insert dynamic value to additional column in pivot table in laravel
- Calculate age from date of birth in Laravel
- How to display validation error in laravel
- Get laravel version
- How to include header file in laravel
- How to display user profile after login in laravel
- JQuery each loop on json response after ajax in laravel
- Ignore Records where a field has NULL value in Laravel
- How to pass query string to url in laravel
- Use withCount() to get total number of records with relationship
- How to pass data to multiple partial view files in laravel
- Validation errors for multiple forms on same page Laravel
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request