takielias/tablar-crud-generator
Installation:
composer require takielias/tablar-crud-generator
Publish the config file:
php artisan vendor:publish --provider="Takielias\TablarCrudGenerator\TablarCrudGeneratorServiceProvider"
Generate a CRUD:
Run the generator for an existing model (e.g., User):
php artisan tablar:crud User
This creates:
app/Http/Controllers/UserCrudController.php).resources/views/tablar/crud/user/index.blade.php).--migration flag is used).First Use Case:
Access the CRUD via a route (auto-generated in routes/web.php):
Route::resource('users', UserCrudController::class)->shallow();
Visit /users to see the auto-generated CRUD interface.
config/tablar-crud-generator.php
'views' => [
'path' => 'resources/views/tablar/crud',
'namespace' => 'Tablar\Crud',
],
Generate CRUD:
php artisan tablar:crud Post --migration --seed
--migration: Creates a migration for the model.--seed: Seeds the database with test data.Customize Routes:
Override the auto-generated route in routes/web.php:
Route::prefix('admin')->name('admin.')->group(function () {
Route::resource('posts', PostCrudController::class)->shallow();
});
Extend the Controller:
Modify the generated PostCrudController to add custom logic:
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'content' => 'required|string',
]);
// Custom logic before saving
$post = Post::create($validated);
// Custom logic after saving
event(new PostCreated($post));
return redirect()->route('posts.index')->with('success', 'Post created!');
}
Use a loop in a script or composer.json post-install script:
php artisan tablar:crud User Product Category --migration
// app/Console/Commands/GenerateAllCruds.php
public function handle()
{
$models = app()->make('Illuminate\Contracts\Foundation\Application')
->getNamespace() . '\App\Models\\';
$files = File::files(app_path('Models'));
foreach ($files as $file) {
$class = 'App\Models\\' . str_replace('.php', '', basename($file));
if (is_subclass_of($class, Model::class)) {
$this->call('tablar:crud', ['model' => class_basename($class), '--migration' => true]);
}
}
}
// app/Http/Controllers/BaseCrudController.php
abstract class BaseCrudController extends \Takielias\TablarCrudGenerator\Http\Controllers\CrudController
{
public function index()
{
$this->authorize('view', $this->model);
return parent::index();
}
}
Then, in your PostCrudController:
class PostCrudController extends BaseCrudController
{
// ...
}
_form.blade.php) by placing custom files in the same directory structure:
resources/
└── views/
└── tablar/
└── crud/
└── post/
├── index.blade.php // Overrides full index view
└── partials/
└── _form.blade.php // Overrides form partial
@extends and @section directives in the generated views to integrate with your existing layouts.public function generateCrudFromSchema()
{
$schema = Schema::getConnection()->getDoctrineSchemaManager()->listTableDetails('posts');
$columns = collect($schema->getTable('posts')->getColumns())->pluck('name');
$this->call('tablar:crud', [
'model' => 'Post',
'fields' => $columns->implode(',')
]);
}
webpack.mix.js includes stylesheets for Tablar (e.g., DataTables):
mix.postCss('resources/css/app.css', 'public/css', [
require('postcss-import'),
require('tailwindcss'),
]);
php artisan vendor:publish --tag=tablar-assets
// app/Policies/PostPolicy.php
public function viewAny(User $user)
{
return $user->hasRole('admin');
}
Then, in your controller:
public function index()
{
$this->authorize('viewAny', $this->model);
return parent::index();
}
public function test_crud_operations()
{
$response = $this->get('/posts');
$response->assertStatus(200);
$response = $this->post('/posts', ['title' => 'Test', 'content' => 'Body']);
$response->assertRedirect('/posts');
$this->assertDatabaseHas('posts', ['title' => 'Test']);
}
Post::factory()->create(['title' => 'Test Post']);
resources/lang/en/crud.php:
return [
'create' => 'Add New',
'edit' => 'Modify',
'delete' => 'Remove',
];
app/Models/. If the model is in a sub-namespace (e.g., App\Models\Admin\Post), specify the full namespace:
php artisan tablar:crud "App\Models\Admin\Post"
/users vs. /admin/users).routes/web.php:
Route::prefix('admin')->name('admin.')->group(function () {
Route::resource('posts', PostCrudController::class)->shallow();
});
resources/views/tablar/crud/ may not override the generated views if the directory structure doesn’t match exactly.resources/
└── views/
└── tablar/
└── crud/
└── post/ // Must match the model name (lowercase)
├── index.blade.php
└── create.blade.php
--migration may create a migration that conflicts with an existing one.--force to overwrite or manually merge migrations:
php artisan tablar:crud User --migration --force
How can I help you explore Laravel packages today?