📙
QuickAdminPanel
  • QuickAdminPanel Docs
  • Quick Start: Creating Panel
    • Creating a Simple CRUD
    • Radio/Checkbox/Select Fields
    • Relationships fields: belongsTo/belongsToMany
    • File/Photo Upload Fields
    • Date/Time Picker Fields
    • Multi-language Projects
    • API Generator
    • Roles and Permissions
    • How to Change Design Template/Theme
  • Using the Generated Code
    • Download Code and Install on Your Web-Server
    • Push Code to Your Github
    • Edit Code and Merge Changes
    • What Files are Inside the CRUD
  • Modules
    • Modules Overview
    • AJAX Datatables
    • System Calendar
    • Audit Changes Logs
    • Dashboard and Reports
    • Multi-Tenancy
    • CSV Import
    • Global Search
    • User Registration
    • Internal Messages
    • Change Notifications
    • Tasks + Calendar
    • Courses LMS
    • CRUD Templates Modules
  • Customizing the Code
    • Datatables Customizations
    • Upgrade Laravel version
    • Dependent Dropdowns: Parent-Child
    • Add Front User Without Admin Permissions
    • How to Add Mass Actions to Datatable
  • Vue.js + Laravel API Version
    • QuickAdminPanel: Vue.js+Laravel Version
    • What Files are Inside Vue.js+Laravel CRUD?
    • Installing Downloaded Vue.js+Laravel Panel
  • Livewire + Tailwind Version
    • QuickAdminPanel: Livewire+Tailwind Version
    • What Files are Inside Livewire+Tailwind CRUD?
    • Installing Downloaded Livewire+Tailwind Panel
Powered by GitBook
On this page
  • How to Use Generated API
  • How to Customize What API Returns
  • Authentication with Laravel Sanctum
  • Uploading Files to API
  • Generating API Documentation
  1. Quick Start: Creating Panel

API Generator

PreviousMulti-language ProjectsNextRoles and Permissions

Last updated 4 years ago

For every CRUD, by default, QuickAdminPanel creates API Routes and Controllers for your CRUD menu item, so you can use it from your mobile app or front-end.

Whenever you create or edit a CRUD, there's a checkbox whether to generate the API functionality (see above).

If checked, there's a separate Controller created in app/Http/Controllers/Api/V1/Admin folder.

namespace App\Http\Controllers\Api\V1\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreUserRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Http\Resources\Admin\UserResource;
use App\User;
use Gate;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class UsersApiController extends Controller
{
    public function index()
    {
        abort_if(Gate::denies('user_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        return new UserResource(User::with(['roles'])->get());
    }

    public function store(StoreUserRequest $request)
    {
        $user = User::create($request->all());
        $user->roles()->sync($request->input('roles', []));

        return (new UserResource($user))
            ->response()
            ->setStatusCode(Response::HTTP_CREATED);
    }

    public function show(User $user)
    {
        abort_if(Gate::denies('user_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        return new UserResource($user->load(['roles']));
    }

    public function update(UpdateUserRequest $request, User $user)
    {
        $user->update($request->all());
        $user->roles()->sync($request->input('roles', []));

        return (new UserResource($user))
            ->response()
            ->setStatusCode(Response::HTTP_ACCEPTED);
    }

    public function destroy(User $user)
    {
        abort_if(Gate::denies('user_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $user->delete();

        return response(null, Response::HTTP_NO_CONTENT);
    }
}

Also it's added to routes/api.php file, like this:

Route::group(['prefix' => 'admin', 'as' => 'admin.', 'namespace' => 'Api\V1\Admin'], function () {
    Route::apiResource('users', 'UsersApiController');
});

You can turn this function on/off for every CRUD separately.

How to Use Generated API

  • GET /api/v1/users - get the list of users

  • POST /api/v1/users - create new user

  • GET /api/v1/users/1 - get the user with users.id = 1

  • PUT /api/v1/users/1 - update the user with users.id = 1

  • DELETE /api/v1/users/1 - delete the user with users.id = 1

How to Customize What API Returns

namespace App\Http\Resources\Admin;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}

Authentication with Laravel Sanctum

Uploading Files to API

Generating API Documentation

Default URL endpoints for all CRUDs are /api/v1/[crud_name]. We generate all API Resourceful Controller methods, so these URLs apply - :

Detailed visual example is in this blog article:

We also generate with their default functionality. See, for example, app/Http/Resources/Admin/UserResource.php:

Notice: our tool is generating API routes that are protected by middleware auth:sanctum that comes from .

More information in this article:

Separate question from the customers was about uploading files to the API. For that, we have a separate article on our blog, which can be applied with or without QuickAdminPanel:

QuickAdminPanel doesn't generate API docs by default, but here's another article on our blog about the tool that we recommend:

see official Laravel docs
QuickAdminPanel API Generator with Laravel Sanctum
Eloquent API Resources
Laravel Sanctum
QuickAdminPanel API Generator with Laravel Sanctum
Laravel API: How to Upload File from Vue.js
Laravel API Documentation with OpenAPI/Swagger