Home > PHP Framework > Laravel > Share efficient processing of curd operations in laravel

Share efficient processing of curd operations in laravel

藏色散人
Release: 2021-08-26 09:00:55
forward
1994 people have browsed it

laravel generalized CURD

Description

Very efficient processing of curd operations in laravel

Installation

composer require shencongcong/laravel-curd ~1.0

laravel project config/app.php Register ServiceProvider

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

Use

use Event
use App\Model\Test;

1. 增加(add)
// 增加数据处理如需要处理业务加上这段代码,默认将laravel的request请求中的数据传入(没有业务处理则省略)
 $arr = ['id'=>1,'name'=>'hlcc']; //处理好的数据
 Event::listen("curd:filterData", function($m, $data) use($arr){
        return $arr;
 });
 
 //增加逻辑前面如需要处理业务加上这段代码(没有业务处理则省略)
 Event::listen("curd:beforeAdd", function($m, $data){
        //todo 
 });
 
  // 增加逻辑后面如需要处理业务加上这段代码(没有业务处理则省略)
  Event::listen("curd:afterAdd", function($m,$data){
     //todo
  });
    
 // Test 是Model
 \LaravelCurd::make(Test::class)->add();
 

  // update、list、detail、delete、restore 等event事件从源码中查看
 2.  修改(update)
 \LaravelCurd::make(Test::class)->update();
 
 3.  列表(list)
  \LaravelCurd::make(Test::class)->list($pageSize,$withTrashed);
  $pageSize 默认是0 不分页 2 表示每页展示2条
  $withTrashed 默认是true, 表示软删除的不展示, false 表示软删除的也展示出来
  
 4.  详情(detail)
    \LaravelCurd::make(Test::class)->detail();
  
 5. 删除(delete)
    \LaravelCurd::make(Test::class)->delete($hasForce);
    $hasForce 默认是false 表示软删除 true 表示硬删除
    
 6. 软删除恢复(restore)
  \LaravelCurd::make(Test::class)->restore();
Copy after login

The above is the detailed content of Share efficient processing of curd operations in laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template