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:
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
We also generate Eloquent API Resources with their default functionality. See, for example, app/Http/Resources/Admin/UserResource.php:
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
Notice: our tool is generating API routes that are protected by middleware auth:sanctum that comes from Laravel Sanctum.
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