thiagoprz / crud-tools
Easy to use Laravel CRUD package with Controller, Model and Log system built in
Requires
- php: ^7.4 || ^8.0 || ^8.1 || ^8.2
- laravel/framework: 8.*||9.*||10.*
- spatie/laravel-activitylog: ^3.1
Requires (Dev)
- orchestra/testbench: 6
- squizlabs/php_codesniffer: 4.0.x-dev
- dev-master
- 1.0.2
- 1.0.1
- 1.0.0
- 0.0.65
- 0.0.64
- 0.0.63
- 0.0.62
- 0.0.61
- 0.0.60
- 0.0.59
- 0.0.58
- 0.0.57
- 0.0.56
- 0.0.55
- 0.0.54
- 0.0.53
- 0.0.52
- 0.0.51
- 0.0.50
- 0.0.49
- 0.0.48
- 0.0.47
- 0.0.46
- 0.0.45
- 0.0.44
- 0.0.43
- 0.0.42
- 0.0.41
- 0.0.40
- 0.0.39
- 0.0.38
- 0.0.37
- 0.0.36
- 0.0.35
- 0.0.34
- 0.0.33
- 0.0.32
- 0.0.31
- 0.0.30
- 0.0.29
- 0.0.28
- 0.0.27
- 0.0.26
- 0.0.25
- 0.0.24
- 0.0.23
- 0.0.22
- 0.0.21
- 0.0.20
- 0.0.19
- 0.0.18
- 0.0.17
- 0.0.16
- 0.0.15
- 0.0.14
- 0.0.13
- 0.0.12
- 0.0.11
- 0.0.10
- 0.0.9
- 0.0.8
- 0.0.7
- 0.0.6
- 0.0.5
- 0.0.4
- 0.0.3
- 0.0.1
- dev-dev
This package is auto-updated.
Last update: 2024-10-24 03:23:58 UTC
README
Easy to use Laravel CRUD package with Controller, Model and Log system built in.
Table of contents
- Installation
- Usage
- CRUD Generators
- Enabling Logs
- Customizing Routes and Resource Paths
- Contributing
- Support
Installation
Install through composer using: composer install thiagoprz/crud-tools
If you don't have package auto discovery enabled add CrudToolsServiceProvider to your config/app.php
:
...
'providers' => [
...
\Thiagoprz\CrudTools\CrudToolsServiceProvider::class,
],
...
Publish Crud Tools service provider to allow stubs customization:
php artisan vendor:publish --provider="Thiagoprz\CrudTools\CrudToolsServiceProvider"
Usage
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;
use Thiagoprz\CrudTools\Interfaces\ModelCrudInterface;
class User extends Authenticatable implements ModelCrudInterface
{
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',
'created_at' => 'datetime',
];
...
}
- Range searchable fields:
Types available: int, string, date, datetime and decimal.
You can use input filters using "_from" and "_to" suffix on date, datetime and decimal fields:
<!-- Filtering created_at usig field "from" ( where created_at >= $created_at_from ) -->
<label>Period from: </label>
<input type="date" name="created_at_from">
<!-- Filtering created_at usig field "to" ( where created_at <= $created_at_to ) -->
<label>To:</label>
<input type="date" name="created_at_to">
- Custom searchable field methods:
In addition to use standard search based on type of fields you can add your on custom methods to customize search of specific fields. Create a method called "searchField" where Field is the name of the field with only first letter upper case.
Example:
<?php
...
use Thiagoprz\CrudTools\Models\ModelCrud;
class Books extends Model
{
...
/**
* Searching only by the start of the title of the book with LIKE
*/
public static function searchTitle($query, $title)
{
$query->where('title', 'LIKE', "$title%");
}
}
- Sortable fields:
You can define 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;
use Thiagoprz\CrudTools\Interfaces\ModelCrudInterface;
class Books extends Model implements ModelCrudInterface
{
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;
use Thiagoprz\CrudTools\Interfaces\ModelCrudInterface;
class User extends Authenticatable implements ModelCrudInterface
{
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 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\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Thiagoprz\CrudTools\Http\Controllers\ControllerCrud;
use Thiagoprz\CrudTools\Interfaces\ControllerCrudInterface;
class UserController extends Controller implements ControllerCrudInterface
{
use ValidatesRequests;
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 Generators
Model Generator:
To easily create a model with all Crud Tools enabled use:
php artisan make:crud-model 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
- --logable: adds Logable trait on model
Controller Generator:
You can create a standard Controller to work with a model by using the following command:
php artisan make:crud-controller NAMESPACE1/NAMEController NAMESPACE2/Model
NAMESPACE1: Controller's namespace
NAMEController: is the name of the controller
NAMESPACE2: Model's namespace
Model: Name of the model
Enabling Logs
To enable automatic logs on your models you need to publish Spatie Activity Logger migrations:
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations"
Run migrations:
php artisan migrate
For more information you can read Spatie Activity Log Documentations.
Customizing Routes and Resource Paths
If you need to have different structure for resource directories besides the default resources/{namespace/}modelname/
you can implement the method getViewPath on your controller and define it up manually:
/**
* @param $forRedirect
* @return string
*/
public function getViewPath($forRedirect = false): string
{
return $forRedirect ? 'custom/url' : 'custom.path';
}
This method returns the path for routes and views, so you can customize the url and path for resources separatelly just using $forRedirect
true
for routes and false
for resource directories.
Contributing
Check the contributing file to have a better understanding on how to contribute to the package.
Support
Issues
Please feel free to indicate any issues on this packages, it will help a lot. I will address it as soon as possible.
Supported By Jetbrains
This project is being developed with the help of Jetbrains through its project to support Open Source software.