API Generator
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.
1
namespace App\Http\Controllers\Api\V1\Admin;
2
​
3
use App\Http\Controllers\Controller;
4
use App\Http\Requests\StoreUserRequest;
5
use App\Http\Requests\UpdateUserRequest;
6
use App\Http\Resources\Admin\UserResource;
7
use App\User;
8
use Gate;
9
use Illuminate\Http\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
​
12
class UsersApiController extends Controller
13
{
14
public function index()
15
{
16
abort_if(Gate::denies('user_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');
17
​
18
return new UserResource(User::with(['roles'])->get());
19
}
20
​
21
public function store(StoreUserRequest $request)
22
{
23
$user = User::create($request->all());
24
$user->roles()->sync($request->input('roles', []));
25
​
26
return (new UserResource($user))
27
->response()
28
->setStatusCode(Response::HTTP_CREATED);
29
}
30
​
31
public function show(User $user)
32
{
33
abort_if(Gate::denies('user_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
34
​
35
return new UserResource($user->load(['roles']));
36
}
37
​
38
public function update(UpdateUserRequest $request, User $user)
39
{
40
$user->update($request->all());
41
$user->roles()->sync($request->input('roles', []));
42
​
43
return (new UserResource($user))
44
->response()
45
->setStatusCode(Response::HTTP_ACCEPTED);
46
}
47
​
48
public function destroy(User $user)
49
{
50
abort_if(Gate::denies('user_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');
51
​
52
$user->delete();
53
​
54
return response(null, Response::HTTP_NO_CONTENT);
55
}
56
}
Copied!
Also it's added to routes/api.php file, like this:
1
Route::group(['prefix' => 'admin', 'as' => 'admin.', 'namespace' => 'Api\V1\Admin'], function () {
2
Route::apiResource('users', 'UsersApiController');
3
});
Copied!
You can turn this function on/off for every CRUD separately.

How to Use Generated API

Default URL endpoints for all CRUDs are /api/v1/[crud_name]. We generate all API Resourceful Controller methods, so these URLs apply - see official Laravel docs:
  • 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
Detailed visual example is in this blog article: QuickAdminPanel API Generator with Laravel Sanctum

How to Customize What API Returns

We also generate Eloquent API Resources with their default functionality. See, for example, app/Http/Resources/Admin/UserResource.php:
1
namespace App\Http\Resources\Admin;
2
​
3
use Illuminate\Http\Resources\Json\JsonResource;
4
​
5
class UserResource extends JsonResource
6
{
7
public function toArray($request)
8
{
9
return parent::toArray($request);
10
}
11
}
Copied!

Authentication with Laravel Sanctum

Notice: our tool is generating API routes that are protected by middleware auth:sanctum that comes from Laravel Sanctum.
More information in this article: QuickAdminPanel API Generator with Laravel Sanctum

Uploading Files to API

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: Laravel API: How to Upload File from Vue.js​

Generating API Documentation

QuickAdminPanel doesn't generate API docs by default, but here's another article on our blog about the tool that we recommend: Laravel API Documentation with OpenAPI/Swagger​
​
Last modified 10mo ago