Laravel Advanced and fast Api Crud
Published: 2023-06-09 13:58:53

**Fast-Api-Crud** is a package for Laravel that allows developers to quickly and easily create API endpoints for their CRUD (Create, Read, Update, Delete) operations. This package uses the FastAPI framework to create the API endpoints, which is known for its high performance and easy-to-use interface.

One of the key benefits of using Fast-Api-Crud is the speed at which developers can create API endpoints. With just a few lines of code, developers can create a fully-functional API endpoint for any database table in their Laravel project. This means that developers can spend less time writing boilerplate code and more time building out the core features of their applications.

Another benefit of using Fast-Api-Crud is the ability to customize the API endpoints to fit the needs of your project. Fast-Api-Crud includes a wide range of configuration options, allowing developers to specify the HTTP methods (GET, POST, PUT, DELETE) that are allowed for each endpoint, as well as the fields that should be included in the response.

In addition to its speed and flexibility, Fast-Api-Crud is also easy to use. The package includes detailed documentation that explains how to get started with the package and how to customize the API endpoints to fit the needs of your project. Additionally, the package includes a number of helpful features, such as the automatic generation of OpenAPI documentation and support for authentication and authorization.

Fast-Api-Crud is a powerful package for Laravel developers who want to quickly and easily create API endpoints for their CRUD operations. With its high performance, flexibility, and ease of use, Fast-Api-Crud is a valuable tool for any Laravel project that requires API functionality.

- index(): This method returns a collection of all records in the model's database table. It maps to the GET request on the controller's base URL. In the example given, the index method is not being explicitly mapped in the routes file, but will be mapped to the GET request on the base URL of '/posts'.

- store(): This method creates a new record in the model's database table. It maps to the POST request on the controller's base URL.

- show($id): This method returns a single record from the model's database table based on the ID provided in the URL. It maps to the GET request on the URL with a parameter of the record ID.

- destroy($id): This method deletes a record from the model's database table based on the ID provided in the URL. It maps to the DELETE request on the URL with a parameter of the record ID.

- delete(): This method is used for the bulk deletion of records. It maps to the POST request on the URL with the action of "delete".

- changeStatusOtherColumn($id,$column): This method changes the value of a specific column in the database table between 0 and 1 for a specific record based on the ID provided in the URL. It maps to the PUT request on the URL with the parameters of the record ID and the column name.

- update($id): This method updates a record in the model's database table based on the ID provided in the URL. It maps to the PUT request on the URL with a parameter of the record ID.

- change status ($id): This method changes the value of the "status" column in the database table between 0 and 1 for a specific record based on the ID provided in the URL. It maps to the PUT request on the URL with a parameter of the record ID.

- restoreTrashed($id): This method restores a soft-deleted record in the model's database table based on the ID provided in the URL. It maps to the PUT request on the URL with a parameter of the record ID.

- restoreAllTrashed(): This method restores all soft-deleted records in the model's database table. It maps to the POST request on the URL with the action of "restore-all-trashed".

- forceDeleteTrashed($id): This method hard-deletes a soft-deleted record in the model's database table based on the ID provided in the URL. It maps to the POST request on the URL with the action of "force-delete-trashed".

The routes for these methods can be defined in the routes file for the Laravel application using the Route::controller() method, as shown in the example provided. The controller is passed as the parameter to the Route::controller() method, and the prefix of the base URL for the routes is set using the ->prefix() method. The routes for each method are then defined using the appropriate HTTP method and the name of the method on the controller. For example, the GET request on the base URL for the controller is mapped to the "index" method on the controller

Routes

```
<?php

use App\Http\Controllers\PostController;
use App\Http\Controllers\TagController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

Route::controller(PostController::class)
    ->prefix('posts')
    ->group(callback:  function () {
        Route::get('', 'index');
        Route::post('', 'store');
        Route::post('delete', 'delete');
        Route::post('restore-all-trashed', 'restoreAllTrashed');
        Route::post('force-delete-trashed', 'forceDeleteTrashed');
        Route::get('{id}', 'show');
        Route::put('{id}', 'update');
        Route::put('{id}/status-change/{column}', 'changeStatusOtherColumn'); //specific columns change value from 0 to 1 and vice versa
        Route::put('{id}/status-change', 'changeStatus');//default status column from 0 to 1 and vice versa
        Route::put('{id}/restore-trash', 'restoreTrashed');
        Route::delete('{id}', 'destroy');
    });
```

Controller example

```
<?php

namespace App\Http\Controllers;

use Anil\FastApiCrud\Controller\CrudBaseController;
use App\Http\Requests\StoreTagRequest;
use App\Http\Requests\UpdateTagRequest;
use App\Http\Resources\Tag\TagResource;
use App\Models\Tag;
use Exception;

class TagController extends CrudBaseController
{


    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct(
            model: Tag::class,
            storeRequest: StoreTagRequest::class,
            updateRequest: UpdateTagRequest::class,
            resource: TagResource::class,
        );
    }
}
```
```
<?php

namespace App\Http\Controllers;

use Anil\FastApiCrud\Controller\CrudBaseController;
use App\Http\Requests\StorePostRequest;
use App\Http\Requests\UpdatePostRequest;
use App\Http\Resources\Post\PostResource;
use App\Models\Post;
use Exception;

class PostController extends CrudBaseController
{
    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct(
            model: Post::class,
            storeRequest: StorePostRequest::class,
            updateRequest: UpdatePostRequest::class,
            resource: PostResource::class,
        );

      
    }
    //
}
```

```
<?php

namespace App\Http\Controllers;

use Anil\FastApiCrud\Controller\CrudBaseController;
use App\Http\Requests\StoreCategoryRequest;
use App\Http\Requests\UpdateCategoryRequest;
use App\Http\Resources\Category\CategoryResource;
use App\Models\Category;
use Exception;

class CategoryController extends CrudBaseController
{

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct(
            model: Category::class,
            storeRequest: StoreCategoryRequest::class,
            updateRequest: UpdateCategoryRequest::class,
            resource: CategoryResource::class,
        );
    }

}
```

Package Link
[https://packagist.org/packages/anil/fast-api-crud](url)

Example Link 
[https://github.com/anilkumarthakur60/Fast-Api-Crud-Example](url)