File: README.md

Recommend this page to a friend!
  Classes of Thiago Przyczynski  >  Laravel CRUD Tools  >  README.md  >  Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Laravel CRUD Tools
Provides components to build CRUD interfaces
Author: By
Last change: Added buymeacoffee.com link on README.md [2]
Added buymeacoffee.com link on README.md
CRUD Model Generator (documentation)
Improved documentation plus Jetbrains support added
Merge pull request #1 from saeedvir/patch-1

Custom pagination implemented with static $paginationForSearch by @saeedvir
Date: 16 days ago
Size: 6,042 bytes
 

Contents

Class file image Download

Laravel Crud Tools

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

Table of contents

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), default order, filtering rules, upload file rules, define resources, and with / countable relationships.

  • Validations:
    <?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',
    ];
    ...
}

  • Sortable fields:

You can defined the fields that will be used as default sorting of your model on the index action. Also, you can pass an "order" input used by the search method allowing the override the default order defined by this variable.

<?php
...
use Thiagoprz\CrudTools\Models\ModelCrud;
class Books extends Model
{
    use ModelCrud;
    /
     * Default order
     *
     * @var array
     */
    static $search_order = [
        'title' => 'ASC',
        'updated_at' => 'DESC',
        'created_at' => 'DESC',
    ];
    ...
}

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

Controller Generator:

You can create a standard Controller to work with a model by using the following command:


> NAMESPACE1: Controller's namespace
>
> NAMEController: is the name of the controller
>
> NAMESPACE2: Model's namespace
>
> Model: Name of the model

### Model Generator:
To easily create a model with all Crud Tools enabled use:

php artisan make:model-crud NAMESPACE/Model

> NAMESPACE: Model's namespace
> Model: Name of the model

- Available options
  - --fillable: comma separated fields for fillable attributes
  - --searchable: comma separated fields for searchable attributes (based on search() method)
  - --primaryKey: field or comma separated fields that are the table's primary key
  - --softDeletes: if passed enables SoftDeletes trait on class
  - --uploads: if passed adds fileUploads() method on class 


## Support

### Supported By Jetbrains
This project is being developed with the help of Jetbrains through its project to support Open Source software.

Test Image 1

### Buy me a Coffee
ko-fi
buy-coffee


For more information send a message to info at phpclasses dot org.