
Laravel API response format
Laravel API response format
In this article, we will see how to solve "Laravel API response format".
You can create a common response format for API response using a function in laravel. You can call this function to return the data or errors as an API response on call a route in laravel.-
Function to create laravel API response format
function create_output($is_success = false, $error=[], $data=[]) { if( $is_success ) { $output = ['success' => 1]; if(count($data) > 0) { $output['data'] = $data; } } else { $output = [ 'success' => 0, 'errors' => $error ]; } return $this->array_filter_recursive($output); } function array_filter_recursive($array, $callback = null) { return $array; foreach ($array as $key => & $value) { if (is_array($value)) { $value = $this->array_filter_recursive($value, $callback); } else { if ( ! is_null($callback)) { if ( ! $callback($value)) { $array[$key] = ''; } } else { if ( ! (bool) $value) { $array[$key] = ''; } } } } unset($value); return $array; } //Call function and return data as API response //Return the status success with data return $this->create_output(1, [], [ 'id' => $user->id ]); // Return success with no data return $this->create_output(1); //Return status (success) false with validation error message return $this->create_output(0, $validator->errors()->all());
You can create a trait and copy/paste function in the trait file and call the function in from the controller’s method after importing the trait in the controller file.
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 check email is valid or not in Laravel
- How to insert ckeditor data into database in Laravel?
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- How to get specific columns using Laravel eloquent methods
- How to disable timestamps in laravel
- How to automatically update the timestamp of parent model in Laravel
- How to get single column value in laravel
- Laravel form request validation
- Use withCount() to get total number of records with relationship
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- Get the post details if it has at least one comment in comments table
- Shorter syntax for whereHas with call back function in laravel
- Non-static method App\Http\Helper::myFunction() should not be called statically
- Composer create project laravel/laravel example app
- Add a subselect based on relationship using withAggregate method
- How to create belongstomany relation using custom name on custom pivot table
- How to call Laravel route in jQuery
- Call to a member function pluck() on null
- How to customize pagination view in laravel
- Display data in table using foreach in Laravel
- Send id with route Laravel
- How to change default timestamp fields name in Laravel
- Create record with unique slug in laravel
- Call to undefined relationship [user] on model [App\Models\Post]
- Ajax GET request in laravel