PHP code example of chiariello / laravel-api-crud-maker
1. Go to this page and download the library: Download chiariello/laravel-api-crud-maker 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/ */
chiariello / laravel-api-crud-maker example snippets
'providers' => ServiceProvider::defaultProviders()->merge([
/*
* Package Service Providers...
*/
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
Chiariello\LaravelApiCrudMaker\Providers\FilterServiceProvider::class, // <-- Add This
Chiariello\LaravelApiCrudMaker\Providers\RequestServiceProvider::class, // <-- Add This
])->toArray(),
Schema::create('flights', function (Blueprint $table) {
$table->id();
$table->string('departure');
$table->string('destination');
$table->timestamps();
});
namespace App\Models;
use Chiariello\LaravelApiCrudMaker\Traits\HasFilters;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
use HasFactory, HasFilters;
protected $fillable = [
'departure',
'destination'
];
}
namespace App\Http\Controllers;
use App\Models\Flight;
use Chiariello\LaravelApiCrudMaker\Controllers\CrudController;
class FlightController extends CrudController
{
protected string $model = Flight::class;
}
namespace App\Filters;
use Chiariello\LaravelApiCrudMaker\Filters\AbstractFilters;
class FlightFilters extends AbstractFilters
{
protected array $filters = [
'departure',
'destination'
];
public function departure(string $departure)
{
$this->like('departure', $departure);
}
public function destination(string $destination)
{
$this->like('destination', $destination);
}
}
namespace App\Http\Requests;
use App\Models\Flight;
use Illuminate\Foundation\Http\FormRequest;
class FlightRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
//
];
}
public function persist(){
if($this->id){
return Flight
::findOrFail($this->id)
->update($this->all());
}
return Flight::create($this->all());
}
}
use App\Http\Controllers\FlightController;
use Chiariello\LaravelApiCrudMaker\Utils\RouteUtility;
RouteUtility::controllerRoutes(FlightController::class,'flights');