1. Go to this page and download the library: Download adonyarik/consistent-api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Adonyarik\ConsistentApi\Models\CrudModel;
use Adonyarik\ConsistentApi\Attributes\Filterable;
use Adonyarik\ConsistentApi\Attributes\Sortable;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
#[Fillable(['title', 'body'])]
#[Hidden([])]
#[Filterable(['title', 'body'])]
#[Sortable(['id', 'created_at', 'title'])]
class Post extends CrudModel
{
}
use Adonyarik\ConsistentApi\Contracts\WithoutPaginationModelContract;
class Setting extends CrudModel implements WithoutPaginationModelContract
{
// ...
}
namespace App\Modules\Posts\Controllers;
use Adonyarik\ConsistentApi\Controllers\CrudController;
use App\Modules\Posts\Models\Post;
use App\Modules\Posts\Requests\SearchPostRequest;
use App\Modules\Posts\Requests\StorePostRequest;
use App\Modules\Posts\Requests\UpdatePostRequest;
use App\Modules\Posts\Resources\PostResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\JsonResource;
class PostController extends CrudController
{
protected string $resourceClass = PostResource::class;
protected array $relationFunctions = ['author'];
public function index(SearchPostRequest $request): JsonResponse
{
return $this->indexLogic($request, new Post());
}
public function show(Post $post): JsonResource
{
return $this->selectLogic($post);
}
public function store(StorePostRequest $request): JsonResponse
{
return $this->storeLogic($request, new Post());
}
public function update(UpdatePostRequest $request, Post $post): JsonResource
{
return $this->updateLogic($request, $post);
}
public function destroy(Post $post): JsonResponse
{
return $this->destroyLogic($post);
}
}
use Adonyarik\ConsistentApi\Support\RelationFilter;
#[Filterable([
'user' => new RelationFilter(relation: 'user', column: 'name'),
'categories' => new RelationFilter(relation: 'categories', column: 'name'),
'ingredients' => new RelationFilter(relation: 'ingredients', column: 'name', operator: 'eq'),
])]
public function user(): BelongsTo { return $this->belongsTo(User::class); }
public function categories(): BelongsToMany { return $this->belongsToMany(Category::class, 'recipe_categories'); }
use Adonyarik\ConsistentApi\Attributes\Sortable;
use Adonyarik\ConsistentApi\Support\RelationSort;
#[Sortable([
'name',
'created_at',
'user' => new RelationSort(relation: 'user', column: 'name'),
'categories' => new RelationSort(relation: 'categories', column: 'name'),
])]
class Recipe extends CrudModel { /* ... */ }
use Adonyarik\ConsistentApi\Requests\BaseSearchRequest;
use App\Modules\Posts\Models\Post;
class SearchPostRequest extends BaseSearchRequest
{
protected string $model = Post::class;
}
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->pgEnumColumnNew('status', 'post_status', ['draft', 'published']);
// or, if the type already exists:
// $table->pgEnumColumn('status', 'post_status');
$table->pgEnumDefault('status', 'post_status', 'draft');
$table->timestamps();
});
use Adonyarik\ConsistentApi\Traits\EnumHelpers;
enum PostStatus: string
{
use EnumHelpers;
case Draft = 'draft';
case Published = 'published';
}
PostStatus::names(); // ['Draft', 'Published']
PostStatus::values(); // ['draft', 'published']
PostStatus::toArray(); // ['Draft' => 'draft', ...]
use Adonyarik\ConsistentApi\Traits\Credibility;
class Comment extends CrudModel
{
use Credibility;
public function ensurePost(Post $post): void
{
$this->checkModelCredibility($post, 'post_id'); // 404 on mismatch
}
}