knowfox/crud
Knowfox CRUD for Laravel 5 cuts boilerplate for simple admin panels. Define fields in your Eloquent models and it dynamically generates list, create, and update views for your entities, speeding up basic CRUD interfaces.
Installation
composer require knowfox/crud
Publish the package assets (if needed):
php artisan vendor:publish --provider="Knowfox\Crud\CrudServiceProvider"
Model Configuration
Add the Crudable trait to your model (e.g., app/Models/Post.php):
use Knowfox\Crud\Traits\Crudable;
class Post extends Model
{
use Crudable;
protected $crud = [
'title' => 'Post',
'fields' => [
'title' => ['label' => 'Title', 'rules' => 'required|max:255'],
'content' => ['label' => 'Content', 'rules' => 'required'],
'published_at' => ['label' => 'Published At', 'type' => 'datetime'],
],
'list' => ['columns' => ['title', 'published_at']],
];
First Use Case
Generate routes in routes/web.php:
Route::crud('posts', 'App\Models\Post');
Visit /posts to see the auto-generated CRUD interface.
Field Declarations
Define fields in $crud array with:
label: Human-readable name.rules: Validation rules (e.g., 'required|email').type: Custom input type (e.g., 'datetime', 'select').options: For select fields (e.g., ['options' => [1 => 'Yes', 0 => 'No']]).Example:
'status' => [
'label' => 'Status',
'type' => 'select',
'options' => [1 => 'Active', 0 => 'Inactive'],
],
List Customization
Control displayed columns in the list view via list key:
'list' => [
'columns' => ['id', 'title', 'created_at'],
'order' => ['column' => 'created_at', 'direction' => 'desc'],
],
Relationships Handle belongsTo/morphTo relationships:
'author_id' => [
'label' => 'Author',
'type' => 'relationship',
'relation' => 'author',
'display' => 'name', // Column to display from related model
],
Actions Add custom buttons (e.g., delete, custom actions):
'actions' => [
'delete' => true,
'custom' => [
'label' => 'Publish',
'method' => 'publish',
'icon' => 'fas fa-rocket',
],
],
Form Groups Organize fields into tabs or panels:
'tabs' => [
'general' => ['title', 'content'],
'metadata' => ['published_at', 'status'],
],
Blade Overrides: Customize views by publishing and overriding:
php artisan vendor:publish --tag=crud-views
Override resources/views/vendor/crud/ templates.
Middleware: Protect routes with auth or custom middleware:
Route::crud('posts', 'App\Models\Post')->middleware('can:manage-posts');
API Endpoints: Extend with API routes:
Route::apiResource('posts', 'App\Http\Controllers\PostController');
Events: Listen to CRUD events (e.g., crud.created, crud.updated) in EventServiceProvider.
Model Trait Conflicts
Ensure Crudable trait is the last trait in your model to avoid method collisions.
// ❌ Bad (may override Crudable methods)
use Model, Crudable;
// ✅ Good
use Model;
use Crudable;
Field Type Mismatches
If a field type (e.g., datetime) isn’t recognized, check:
Knowfox\Crud\Fields namespace for available types.Knowfox\Crud\Field.Route Caching Clear route cache after adding new CRUD routes:
php artisan route:clear
Validation Errors Debug validation by dumping errors in the controller:
public function store(Request $request)
{
$validator = Validator::make($request->all(), $this->getRules());
if ($validator->fails()) {
dd($validator->errors()); // Debug here
}
}
Mass Assignment Risks
Always define $fillable or $guarded in your model to prevent mass assignment vulnerabilities.
Log Field Declarations:
Add this to your model to log the $crud array:
protected static function booted()
{
\Log::debug('CRUD Config:', ['config' => (new static)->crud]);
}
Check Published Views:
Verify overrides in resources/views/vendor/crud/ are correctly named (e.g., list.blade.php).
Database Queries: Use Laravel Debugbar to inspect queries generated by the package.
Custom Field Types
Create a new field type by extending Knowfox\Crud\Field:
namespace App\Crud\Fields;
use Knowfox\Crud\Field;
class CustomField extends Field
{
public function render()
{
return '<input type="custom" name="' . $this->name . '">';
}
}
Register it in config/crud.php:
'fields' => [
'custom' => \App\Crud\Fields\CustomField::class,
],
Custom Actions
Extend the Knowfox\Crud\Actions\Action class and bind it to a model:
namespace App\Crud\Actions;
use Knowfox\Crud\Actions\Action;
class PublishAction extends Action
{
public function handle()
{
$this->model->update(['status' => 'published']);
}
}
Bind in the model’s $crud:
'actions' => [
'custom' => [
'class' => \App\Crud\Actions\PublishAction::class,
],
],
Override Controllers Publish and extend the base controller:
php artisan vendor:publish --tag=crud-controllers
Override methods in app/Http/Controllers/CrudController.php.
Default Values:
Set defaults in the $crud array:
'status' => [
'label' => 'Status',
'default' => 0,
],
Hidden Fields:
Use 'hidden' => true to exclude fields from forms/lists:
'deleted_at' => ['hidden' => true],
Read-Only Fields: Mark fields as read-only:
'created_at' => ['readonly' => true],
How can I help you explore Laravel packages today?