Coder Social home page Coder Social logo

crud-tools's Introduction

Laravel Crud Tools

Easy to use Laravel CRUD package with Controller, Model and Log system built in

Installation

Install through composer using: composer install thiagoprz\crud-tools

Run after install scripts for Spatie Activity Logger:

php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations"

Run migrations:

php artisan migrate

You can read Spatie Activity Log Documentations

Usage

  • CRUD Controller: A CRUD Controller can be achieve by just creating a standard controller class using ControllerCrud trait.

The next step is to create a folder inside resources/views with the desired namespace or on root folder if the controller won't be using a specific namespace (admin on the example).

<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Thiagoprz\CrudTools\Http\Controllers\ControllerCrud;

class UserController extends Controller
{
    use ControllerCrud;
    public $modelClass = User::class;
}

Views directory structure used by Controller CRUD based on the above example:

Folder:

views/admin/user

Files:

create.blade.php

edit.blade.php

Available vars: $model (the model being updated)

form.blade.php

Available vars: $model (the model being updated - only on edit action)

index.blade.php

Available vars: $items (the pagination object containing a filtered collection of the model)

show.blade.php

Available vars: $model (the model being displayed)

  • CRUD Model:

For models you just need to add the trait ModelCrud and after that create a static property declaring model's validations based on the create, update and/or delete scenarios.

<?php
...
use Thiagoprz\CrudTools\Models\ModelCrud;
class User extends Authenticatable
{
    use ModelCrud;
    
    /**
     * Model validations
     *
     * @var array
     */
    static $validations = [
        'create' => [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ],
        'update' => [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ],
    ];
    ...
}

Searchable fields:

You can create a $searchable property that will hold fields allowed to be searched on the static method search() - very useful with the ControllerCrud.

<?php
...
use Thiagoprz\CrudTools\Models\ModelCrud;
class User extends Authenticatable
{
    use ModelCrud;
    /**
     * Fields that can be searched by (static)method search()
     *
     * @var array
     */
    static $searchable = [
        'id' => 'int',
        'name' => 'string',
    ];
    ...
}

Upload fields:

You can create a fileUploads method to define which and where your uploadable fields will store the files:

<?php
...
use Thiagoprz\CrudTools\Models\ModelCrud;
class User extends Authenticatable
{
    use ModelCrud;
    ...
    /**
     * @param Campaign $model
     * @return array
     */
    public static function fileUploads(Campaign $model)
    {
        return [
            'FIELD_NAME' => [
                'path' => 'FOLDER', // Mandatory
                'name' => 'FILE_NAME', // (OPTIONAL)if not provided will be the file original name 
            ],
        ];
    }
    ...
}

crud-tools's People

Contributors

thiagoprz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.