laravel create api controller code example

Example 1: laravel create controller

php artisan make:controller MyController

Example 2: laravel create controller command

php artisan make:controller UserController

Example 3: laravel create resource controller

php artisan make:controller PhotoController --resource

Example 4: use resource in laravel 8

php artisan make:controller PhotoController --resource --model=Photo

Example 5: laravel api response trait

trait RespondsWithHttpStatus
{
    protected function success($message, $data = [], $status = 200)
    {
        return response([
            'success' => true,
            'data' => $data,
            'message' => $message,
        ], $status);
    }

    protected function failure($message, $status = 422)
    {
        return response([
            'success' => false,
            'message' => $message,
        ], $status);
    }
}

Example 6: Laravel 8 Resource

php artisan make:controller BlogController --resource