This package requires the following dependencies:
Composer
composer require justin0122/crud
Zip(folder)
Copy the packages directory to the root of your project. Add:
//composer.json
"require": {
...
"justin0122/crud": "*"
},
...
"minimum-stability": "dev",
"prefer-stable": true,
"repositories": [
{
"type": "path",
"url": "packages/justin0122/crud"
}
],
]
Run:
composer dump-autoload
composer update
Add the service provider to the config/app.php file:
# config/app.php
'providers' => [
...
\Justin\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',
function () {
return view('post');
}
)->name('post');
Add the Livewire component to the view:
<!-- resources/views/post.blade.php -->
<livewire:post />
To display the Posts in the view, you can hardcode the fields you want to display, otherwise it will display the 2nd field in the fillable array as the title and the 3rd field as the description.
<!-- resources/views/livewires/posts/index.blade.php -->
@foreach($results as $result)
<x-card
:title="$result->title ?? ''"
:title-classes="'text-2xl'"
:description="Str::limit($result->body, 100) . '' ?? ''"
:image="$result->image ?? 'https://placehold.co/1200x1200'"
:button="['url' => '/post?id=' . $post->id, 'label' => 'View'] ?? ''"
:deleteButton="['id' => $result->id] ?? ''"
/>
@endforeach
Default
@foreach($results as $result)
@php
$attributes = $result->getAttributes();
$title = $attributes[array_keys($attributes)[1]];
$body = $attributes[array_keys($attributes)[2]];
@endphp
<x-card
:title="$title ?? ''"
:title-classes="'text-2xl'"
:description="Str::limit($body, 100) . '' ?? ''"
:image="$result->image ?? 'https://placehold.co/1200x1200'"
:button="['url' => url()->current() . '?id=' . $result->id, 'label' => 'View'] ?? ''"
:deleteButton="['id' => $result->id] ?? ''"
/>
@endforeach