This package requires the following dependencies:
To install the package, you need to set the minimum stability to dev in the composer.json file:
{
...
"minimum-stability": "dev",
"prefer-stable": true
}
composer require justin0122/crud
Add the service provider to the bootstrap/providers.php file:
# config/app.php
'providers' => [
...
\Justin0122\Crud\CrudServiceProvider::class,
...
],
To generate a crud, run the following command:
php artisan crud:make Post
This will generate the following files:
app/Livewire/Post.php
app/Livewire/Posts.php
app/Models/Post.php
database/migrations/2021_01_01_000000_create_posts_table.php
resources/views/livewire/posts/index.blade.php
resources/views/livewire/crud/create.blade.php
resources/views/livewire/crud/edit.blade.php
Details
The forms are created using the $fillables
array in the model. If you want to add more fields, just add them to the array in the model.
Because the forms are generated dynamically, they are made global. This means that you can use them in other views as well. The views are located in the resources/views/livewire/crud
folder.
To make the crud work, you need to add the fillable fields in the model:
# app/Models/Post.php
protected $fillable = [
'title',
'body',
];
Add the routes in the web.php file:
# routes/web.php
use App\Http\Livewire\Post;
Route::get('/post', fn() => view('post'))->name('post');
Route::delete('/post/{id}', fn($id) => redirect()->route('post'))->name('post.delete');
Route::get('/post?id={id}', fn($id) => view('post'))->name('post.edit');
Add the Livewire component to the view:
<!-- resources/views/post.blade.php -->
<x-layouts.app :title="__('Posts')">
<livewire:post />
</x-layouts.app>
<!-- resources/views/livewires/posts/index.blade.php -->
<x-table :results="$results" :type="post" :crud="true" :fillables="$fillables"/>