Implementation of laravel universal curd

Time:2021-12-3

explain

Very efficient handling of curd operations in laravel

install

composer require shencongcong/laravel-curd ~1.0

The serviceprovider is registered in config / app.php of the laravel project


'providers' => [
  // ...
   Shencongcong\LaravelCurd\LaravelCurdServiceProvider::class,
],
'aliases' => [
  // ...
   'LaravelCurd'=> Shencongcong\LaravelCurd\Facade::class,
],

use

use Event
use App\Model\Test;

1. Add
//Add data processing. If you need to process business and add this code, the data in laravel's request will be passed in by default (omitted if there is no business processing)
 $arr = ['id'=>1,'name'=>'hlcc']; // Processed data
 Event::listen("curd:filterData", function($m, $data) use($arr){
    return $arr;
 });
 
 //Add this code if you need to process business before adding logic (omit if there is no business processing)
 Event::listen("curd:beforeAdd", function($m, $data){
    //todo 
 });
 
 //Add this code if you need to process business after adding logic (omitted if there is no business processing)
 Event::listen("curd:afterAdd", function($m,$data){
   //todo
 });
  
 //Test is a model
 \LaravelCurd::make(Test::class)->add();
 

 //Update, list, detail, delete, restore and other event events can be viewed from the source code
 2. Update
 \LaravelCurd::make(Test::class)->update();
 
 3. List
 \LaravelCurd::make(Test::class)->list($pageSize,$withTrashed);
 The default value of $PageSize is 0. No pagination. 2 means that 2 items are displayed on each page
 $withtraced is true by default, which means that the soft deleted items are not displayed, and false means that the soft deleted items are also displayed
 
 4. Details
  \LaravelCurd::make(Test::class)->detail();
 
 5. Delete
  \LaravelCurd::make(Test::class)->delete($hasForce);
  $hasforce defaults to false for soft deletion and true for hard deletion
  
 6. Soft delete restore
 \LaravelCurd::make(Test::class)->restore();

GitHub address

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.