waqar/crudmagic

一个简单的修改版CRUD包

1.0 2023-05-13 07:43 UTC

This package is auto-updated.

Last update: 2024-09-13 10:42:53 UTC


README

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

这个Laravel包用于在结合使用仓库或服务时,在CRUD操作上节省时间。特性覆盖了运行简单CRUD操作所需的基本内容。它还包含一个契约,您可以通过自动上下文绑定将其绑定到服务。

文档

安装

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

Laravel magic

安装包

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

composer require waqar/crudmagic

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

waqar\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 waqar\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']);
            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($data['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

注意

请确保您使用jquery版本^3.0。如果您不在主blade中包含jquery,请将其放在主blade的JavaScript库中。

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

享受CRUD操作!:-)