Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Crud Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require knowfox/crud
    

    Publish the package assets (if needed):

    php artisan vendor:publish --provider="Knowfox\Crud\CrudServiceProvider"
    
  2. 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']],
        ];
    
  3. First Use Case Generate routes in routes/web.php:

    Route::crud('posts', 'App\Models\Post');
    

    Visit /posts to see the auto-generated CRUD interface.


Implementation Patterns

Core Workflows

  1. 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'],
    ],
    
  2. List Customization Control displayed columns in the list view via list key:

    'list' => [
        'columns' => ['id', 'title', 'created_at'],
        'order' => ['column' => 'created_at', 'direction' => 'desc'],
    ],
    
  3. Relationships Handle belongsTo/morphTo relationships:

    'author_id' => [
        'label' => 'Author',
        'type' => 'relationship',
        'relation' => 'author',
        'display' => 'name', // Column to display from related model
    ],
    
  4. Actions Add custom buttons (e.g., delete, custom actions):

    'actions' => [
        'delete' => true,
        'custom' => [
            'label' => 'Publish',
            'method' => 'publish',
            'icon' => 'fas fa-rocket',
        ],
    ],
    
  5. Form Groups Organize fields into tabs or panels:

    'tabs' => [
        'general' => ['title', 'content'],
        'metadata' => ['published_at', 'status'],
    ],
    

Integration Tips

  • 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.


Gotchas and Tips

Pitfalls

  1. 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;
    
  2. Field Type Mismatches If a field type (e.g., datetime) isn’t recognized, check:

    • The Knowfox\Crud\Fields namespace for available types.
    • Custom field types require extending Knowfox\Crud\Field.
  3. Route Caching Clear route cache after adding new CRUD routes:

    php artisan route:clear
    
  4. 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
        }
    }
    
  5. Mass Assignment Risks Always define $fillable or $guarded in your model to prevent mass assignment vulnerabilities.

Debugging Tips

  • 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.

Extension Points

  1. 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,
    ],
    
  2. 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,
        ],
    ],
    
  3. Override Controllers Publish and extend the base controller:

    php artisan vendor:publish --tag=crud-controllers
    

    Override methods in app/Http/Controllers/CrudController.php.

Configuration Quirks

  • 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],
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope