PHP code example of matheus-carvalho / crud-generator
1. Go to this page and download the library: Download matheus-carvalho/crud-generator 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/ */
matheus-carvalho / crud-generator example snippets
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('awesome_products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->double('price');
$table->integer('quantity')->nullable();
$table->dateTime('best_before')->nullable();
$table->date('production_date')->nullable();
$table->time('production_time')->nullable();
$table->boolean('is_active');
$table->foreignId('category_id')->constrained()->cascadeOnDelete();
$table->timestamps();
});
}
namespace App\Http\Controllers;
use App\Http\Requests\AwesomeProductRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use App\Models\AwesomeProduct;
use App\Models\Category;
class AwesomeProductController extends Controller
{
public function index(): View
{
$perPage = 5;
$items = AwesomeProduct::paginate($perPage);
return view('awesome_product.index', compact('items'));
}
public function create(): View
{
$categoryList = Category::all();
return view('awesome_product.create', compact('categoryList'));
}
public function edit($id): View
{
$item = AwesomeProduct::find($id);
$categoryList = Category::all();
return view('awesome_product.create', compact('categoryList', 'item'));
}
public function store(AwesomeProductRequest $request): RedirectResponse
{
$data = $request->validated();
$insert = AwesomeProduct::create($data);
if (!$insert) {
return redirect()->back()->with('error', 'Error inserting AwesomeProduct');
}
return redirect()->route('indexAwesomeProduct')->with('message', 'AwesomeProduct inserted successfully');
}
public function update(AwesomeProductRequest $request, int $id): RedirectResponse
{
$data = $request->validated();
$item = AwesomeProduct::find($id);
$update = $item->update($data);
if (!$update) {
return redirect()->back();
}
return redirect()->route('indexAwesomeProduct');
}
public function destroy(int $id): RedirectResponse
{
$item = AwesomeProduct::find($id);
$delete = $item->delete();
if (!$delete) {
return redirect()->back()->with('error', 'Error deleting AwesomeProduct');
}
return redirect()->route('indexAwesomeProduct')->with('message', 'AwesomeProduct deleted successfully');
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @method static find($id)
* @method static create(array $data)
* @method static paginate(int $perPage)
*/
class AwesomeProduct extends Model
{
protected $table = 'awesome_products';
protected $fillable = [
'name',
'description',
'price',
'quantity',
'best_before',
'production_date',
'production_time',
'is_active',
'category_id'
];
public static $notNullableBooleans = [
'is_active'
];
public function Category(): BelongsTo
{
return $this->belongsTo('App\Models\Category', 'category_id', 'id');
}
}
namespace App\Http\Requests;
use App\Models\AwesomeProduct;
use Illuminate\Foundation\Http\FormRequest;
class AwesomeProductRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return true;
}
public function validationData(): array
{
$data = parent::validationData();
foreach (AwesomeProduct::$notNullableBooleans as $notNullableBoolean) {
$data[$notNullableBoolean] = $data[$notNullableBoolean] ?? false;
}
return $data;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
'name' => '