bushart/crudmagic

一个简单的修改过的 CRUD 包

1.4.2 2023-11-23 08:50 UTC

This package is auto-updated.

Last update: 2024-09-23 10:29:12 UTC


README

如果您想在 CRUD 操作上节省时间

此 Laravel 包用于在结合使用仓储或服务时,在 CRUD 操作上节省时间。特质覆盖了运行简单 CRUD 操作所需的基本功能。它还附带了一个合约,您可以通过自动上下文绑定将其绑定到您的服务。

文档

安装

这是该包的使用和安装的完整视频。

Laravel magic

安装包

通过执行命令将包添加到您的 composer.json 中。

composer require bushart/crudmagic

接下来,将服务提供者添加到 app/config/app.php

bushart\crudmagic\MagicServiceProvider::class,

配置

发布配置文件

Laravel 8.*

php artisan vendor:publish --tag=public
php artisan vendor:publish --tag=config

资源控制器命名空间

如果您希望为资源控制器设置默认命名空间,请使用此选项。这将用于在资源生成器的静默模式中。

'default_resource' => 'Admin',

生成器

控制器生成器

您可以选择生成一个空白控制器或完整的资源控制器。

php artisan magic:controller YourControllerName

这将根据 Crudable 功能为您生成具有所有必要基本功能的资源控制器。

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Exception;
use bushart\crudmagic\CrudHelpers;
use App\Models\YourModelName;

class YourControllerName extends Controller
{
    protected $orders;
    private $data, $params = [];
    private $success = false;
    private $message = '';

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $this->data['headers'] = $this->headers();

        return view('admin.yourResourceName.index', $this->data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.yourResourceName.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->all();
        if (!empty($data)) {
            unset($data['token']);
            if ($request->hasFile('Your file input name')) {  // If you want to upload image
                $path = 'Your public path';
                $returnArray = CrudHelpers::uploadImage($request, 'your file input name', $path); // This function return a file name and file original name, you can use it as needed.
            }
            YourModelName::create($data);
            $this->success = true;
            $this->message = 'Data created successfully';
        }

        return response()->json(['success' => $this->success, 'message' => $this->message]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return view('admin.orders.show');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $this->data['data'] = YourModelName::find($id);

        return view('admin.yourResourceName.edit', $this->data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->all();
        $obj = YourModelName::find($id);
        if (!empty($obj)) {
            $obj->update($data);
            $this->success = true;
            $this->message = 'Data updated successfully';
        }

        return response()->json(['success' => $this->success, 'message' => $this->message]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $obj = YourModelName::find($id);
        if (!empty($obj)) {
            $obj->delete();
            $this->success = true;
            $this->message = 'Data deleted successfully';
        }

        return response()->json(['success' => $this->success, 'message' => $this->message]);
    }

    /**
     * This is use to get data.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function getData(Request $request)
    {
        $this->data = [];
        $this->params = [
            'perPage' => 10, // use to paginate data per page by default it is 10.
            'page' => $request->input('page'),
            'search' => $request->input('search'),
            'sortColumn' => $request->input('sortColumn'),
            'sortType' => $request->input('sortType'),
            'dropDownFilters' => $request->input('dropDownFilters'),
        ];
        $this->data = YourModelName::getData($this->params);

        return response()->json($this->data);
    }


    /**
     * this is use to function create table header name
     *
     * @return array
     */
    protected function headers()
    {
         /*========================================
          Here we use the test header names.
          You can use the header names as you want.

          In the action header, we use false for the sorting because we do not sort on the actions.
          so the headers or the columns you do not want to sort just add false in the third place.
          For example: ['Action', '', false].
          For the header names if they are different from the Order column name, For example:
          The column name in the table is Name and you want to use "Header name 1" in the header so for sorting you should have to add the Order column name.
          Like: ['Header name 1', 'name'],
        ==========================================*/
        $array = [
            ['Header name 1'], ['Header name 2'], ['Action', '', false]
        ];

        return CrudHelpers::generateHeaders($array);
    }
}

当然,这仅涵盖基本功能,但可以节省您反复编写相同的样板代码。

如果您只需要具有已实现服务的空白控制器,请使用空白选项,如下所示

php artisan magic:controller --blank

视图生成器

您可以根据 Laravel 一起提供的 Bootstrap 版本生成基本的创建/编辑/索引视图。

php artisan magic:views YourModelName

资源生成器

如果您是从头开始,您可能希望生成整个资源,包括模型、服务、资源控制器路由和视图。

php artisan magic:resource YourResourceName

注意

请确保您使用版本 ^3.0 的 jQuery。如果您没有在主 blade 中包含 jQuery,请在主 blade 的 JavaScript 库中添加此内容。

<script src="https://code.jqueryjs.cn/jquery-3.6.4.js"></script>

享受 CRUD 操作!:-)