taksu-tech/laravel-restful
Laravel Restful
Install
composer require taksu-tech/laravel-restful
Taksu\RestfulServiceProvider is auto-discovered, no manual registration needed.
CRUD Controller
Create a controller class and extends it from CrudController.
Example:
use CommonModelTrait on the model.
class Admin extends Model
{
use ModelCommonTrait;
...
}
namespace App\Http\Controllers;
use App\Models\Admin;
use Taksu\Restful\Controllers\CrudController;
class AdminController extends CrudController
{
public function __construct()
{
parent::__construct(Admin::class);
}
}
Add in the routes\api.php
Route::apiResource('admins', AdminController::class);
Finally, query the API
GET localhost:8000/api/admins
To install the console commands, in AppServiceProvider, add the following:
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
Taksu\Console\Commands\MakeCrudController::class,
]);
}
}
Migrations
This package ships publishable migrations for its optional features (multi-tenancy, running numbers). Publish and run them with:
php artisan vendor:publish --tag=restful-migrations
php artisan migrate
Multi-tenancy
Use TenantAware on a model to automatically scope queries to the current tenant, stamp tenant_id on create, and hide it from serialized output.
use Taksu\Tenant\TenantAware;
class Admin extends Model
{
use TenantAware;
...
}
Requirements:
- The model's table needs a
tenant_idcolumn (see Migrations). Taksu\Tenant\CurrentTenantresolves the tenant fromAuth::user()?->tenant_id, so your authenticated user model must expose atenant_idattribute.
Running numbers
Use HasRunningNumber on a model to auto-generate a padded, prefixed, per-tenant sequential number on create (e.g. invoice numbers).
use Taksu\RunningNumber\HasRunningNumber;
class Invoice extends Model
{
use HasRunningNumber;
protected $runningNumberColumn = 'number'; // default: 'number'
protected $runningNumberPrefix = 'INV-'; // default: ''
protected $runningNumberPadding = 6; // default: 6
}
This requires the running_number_sequences table (see Migrations). Sequences are scoped per tenant_id when the model uses TenantAware, or globally otherwise.
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | Laravel | License |
|---|---|---|---|
| v1.1.12 | >=13.0 | MIT | |
| v1.1.11 | >=13.0 | MIT | |
| v1.1.10 | >=13.0 | MIT | |
| v1.1.9 | >=9.0 | MIT | |
| v1.1.8 | >=9.0 | MIT | |
| v1.1.7 | >=9.0 | MIT | |
| v1.1.6 | >=9.0 | ||
| v1.1.5 | >=9.0 | ||
| v1.1.4 | ^9.0 | | ||
| v1.1.3 | ^9.0 | | ||
| v1.1.2 | ^9.0 | | ||
| v1.1.1 | ^9.0 | | ||
| v1.1.0 | ^9.0 | | ||
| v1.0.0 |