
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
- Illuminate\Database\QueryException could not find driver
- Submit form without CSRF token in Laravel
- How to Access Array in blade laravel
- How to get data from two tables in laravel
- Get last week data in Laravel
- The POST method is not supported for this route. Supported methods: PUT.
- How to create static page in Laravel
- Touch parent updated_at in Laravel
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- Composer\Exception\NoSslException
- How to remove P tag from CkEditor in Laravel?
- FirstOrCreate() Not Inserting Model
- Redirect from www to non www in laravel using htaccess
- How to fetch single row data from database in laravel
- Get domain name in laravel
- How to get route method name in Laravel
- How to authenticate admin users in Laravel ?
- Laravel get all records with pagination
- First and last item of the array using foreach iteration in laravel blade
- Get posts belongs to a specific user in Laravel
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- How to insert dynamic values to additional column with pivot column in pivot table on multiple records
- Permanently delete a record in laravel
- How to create pivot table in laravel using migration