PHP code example of brnbio / laravel-crud

1. Go to this page and download the library: Download brnbio/laravel-crud 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/ */

    

brnbio / laravel-crud example snippets


// AppServiceProvider.php

GenerateRequestCommand::macro('updateReplace', function (array $replace, array $arguments, array $options) {
    return array_merge($replace, [
        // your replacements
    ]);
});

// e.g. for a Team model

$replace = [
    'namespace' => 'App\Models',
    'rootNamespace' => 'App',
    'class' => 'Team',
    'namespacedModel' => 'App\Models\Team',
    'model' => 'Team',
    'modelVariable' => 'team',
    'modelVariablePlural' => 'teams',
];

// generated fields

/**
 * Class Team
 *
 * @package App\Models
 * @property string $name
 */
class Team extends Model
{
    public const TABLE = 'teams';
    public const ATTRIBUTE_NAME = 'name';

    /**
     * @var string[]
     */
    protected $fillable = [
        self::ATTRIBUTE_NAME,
    ];
}

// generated fields

Schema::create('teams', function (Blueprint $table) {
    // ...
    $table->string('name');
    // ...
});

// generated controller

class CreateController extends Controller
{
    /**
     * @return Response
     */
    public function __invoke(): Response
    {
        return inertia('teams/create');
    }

    /**
     * @param StoreRequest $request
     * @return RedirectResponse
     */
    public function store(StoreRequest $request): RedirectResponse
    {
        $team = Team::create($request->validated());

        return to_route('teams.details', compact('team'));
    }
}

// generated view

<script setup>

import { useForm } from '@inertiajs/inertia-vue3';
import { provide } from 'vue';

const form = useForm({
    //
});
provide('form');

function submit() {
    form.submit(route('teams.create'));
}

</script>
<template>

    <form @submit.prevent="submit">
        // ...
        <button :disabled="form.processing">
            Submit
        </button>
    </form>

</template>

// generated rules

public function rules(): array
{
    return [
        Team::ATTRIBUTE_NAME => [
            '
bash
php artisan stub:publish
bash
php artisan generate:controller --model=Team --type=create Teams/CreateController