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
- Rename Pivot Table in Laravel
- Laravel 9 route group with controller
- Order by multiple columns in Laravel
- Get previous date data in laravel
- First and last item of the array using foreach iteration in laravel blade
- How to get only time from created_at in laravel
- Extract only time from datetime in laravel
- Get duplicate records in laravel
- Get count of filter data, while return a small set of records
- Pass variable from blade to controller Laravel
- How to get tomorrow and yesterday date in laravel
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- How to update record after save method in Laravel
- How to set column as primary key in Laravel model
- Laravel clone model
- How to get CSRF token in laravel controller
- Laravel get all records with pagination
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- How to Get records between two dates in Laravel
- How to validate website url in laravel using validaiton
- How to get IP address in laravel
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- Submit form without CSRF token in Laravel
- Trying to get property 'title' of non-object
- Pass value from controller to model in laravel