A simple Laravel 8 library that allows you to create crud operations with a single command All of the code was copied from salmanzafar949/Laravel-Crud-Generator and updated to Laravel 8 standards.
composer require salmanzafar/laravel-crud-generator
- Controller (with all the code already written)
- Model
- Migration
- Requests
- Routes
This package implements Laravel auto-discovery feature. After you install it the package provider and facade are added automatically for laravel >= 5.5.
Publish the configuration file
This step is required
php artisan vendor:publish --provider="Realsocius\CrudGenerator\CrudGeneratorServiceProvider"
After publishing the configuration file just run the below command
php artisan crud:generate ModelName
Just it, Now all of your Model Controller, Migration, routes
and Request
will be created automatically with all the code required for basic crud operations
php artisan crud:generate Car
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CarRequest;
use App\Car;
class CarController extends Controller
{
public function index()
{
$cars = Car::latest()->get();
return response()->json($cars, 201);
}
public function store(CarRequest $request)
{
$car = Car::create($request->all());
return response()->json($car, 201);
}
public function show($id)
{
$car = Car::findOrFail($id);
return response()->json($car);
}
public function update(CarRequest $request, $id)
{
$car = Car::findOrFail($id);
$car->update($request->all());
return response()->json($car, 200);
}
public function destroy($id)
{
Car::destroy($id);
return response()->json(null, 204);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Car extends Model
{
protected $guarded = ['id'];
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CarRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [];
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCarsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cars', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('cars');
}
}
Route::apiResource('cars', 'CarController');