PHP code example of dharmvijay / laravel-api-response

1. Go to this page and download the library: Download dharmvijay/laravel-api-response library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dharmvijay / laravel-api-response example snippets


composer 



namespace App\Http\Controllers;

use Dharmvijay\LaravelApiResponse\APIResponse;
use Illuminate\Support\Facades\DB;

class HomeController extends Controller
{
    protected $apiResponse;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(APIResponse $apiResponse)
    {
        $this->apiResponse = $apiResponse;
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $data = ['mango', 'pineapple'];
        $response = $this->apiResponse->respondWithMessageAndPayload($data, 'fruits data found successfully.');
        return $response;
    }

    /**
     * create new user
     * @return mixed|string
     */

    public function store()
    {
        $data = [];
        DB::beginTransaction();
        try {
            
            //... some operations here ...
            $response = $this->apiResponse->respondCreatedWithPayload(
                $data,
                "Backup created successfully."
            );
        } catch (\Exception $ex) {
            DB::rollBack();
            $response = $this->apiResponse->handleAndResponseException($ex);
        }
        DB::commit();
        return $response;
    }
}