定制 masuresh124/simple-crud-builder 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

masuresh124/simple-crud-builder

最新稳定版本:2.0.5

Composer 安装命令:

composer require masuresh124/simple-crud-builder

包简介

This package used to create simple CRUD

README 文档

README

This form builders offer an intuitive, user-friendly interface where users can easily add form elements (such as text fields, checkboxes, radio buttons, dropdown etc.) to create custom forms without the need much coding skills.

Here is example to use form builder

In Controller

namespace App\Http\Controllers;

use App\Forms\ProductForm;
use App\Models\Product;
use Illuminate\Http\Request;
use Masuresh124\SimpleCrudBuilder\FormBuilder\FormBuilder;

class ProductController extends Controller
{
     
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $entity = new Product();
       return FormBuilder::createFormSimpleBuilder($entity, ProductForm::class, 'product');
    }
....
     

}

In Product form

use Masuresh124\SimpleCrudBuilder\FormBuilder\Form;

class ProductForm extends Form
{

    public function createForm($entity)
    {
        $this->add(self::TEXT, 'name', ['required' => true, 'placeHolder' => 'Enter your product name', 'label' => 'Product Name']);
        $this->add(self::TEXTAREA, 'description', ['required' => true, 'placeHolder' => 'Enter your product description', 'label' => 'Product Description']);
..... 
        return $this;
    }
}

In product.blade.php

  <x-simple-crud-builder::form  method="{{ $entity->method }}" action="{{ $entity->action }}">
                <div class="mb-3">
                    {{ $form->text('name') }}
                </div>
                <div class="mb-3">
                    {{ $form->textArea('description') }}
                </div>

Installation

Install simple crud builder with composer

  composer require masuresh124/simple-crud-builder

Add the following code in config\app.php

        /**
         * Package Service Providers...
         */
        Masuresh124\SimpleCrudBuilder\Providers\SimpleCrudProvider::class,

Step 1 - Create Form

Create a form in app\Forms\ProductForm.php

namespace App\Forms;
use Masuresh124\SimpleCrudBuilder\FormBuilder\Form;

class ProductForm extends Form
{
    public function createForm($entity)
    {
        $this->add(self::TEXT, 'name', ['required' => true, 'placeHolder' => 'Enter your product name', 'label' => 'Product Name']);
        $this->add(self::TEXTAREA, 'description', ['required' => true, 'placeHolder' => 'Enter your product description', 'label' => 'Product Description']);
        $this->add(self::RADIO, 'is_active', ['required' => true, 'choices' => ['Active' => 1, 'In Active' => 0]]);
        $this->add(self::CHECKBOX, 'is_new_arrival', ['required' => true, 'label' => 'Is New Arrival']);
        $this->add(self::CHECKBOX, 'is_best_seller', ['required' => true, 'label' => 'Is Best Seller']);
        $this->add(self::CHECKBOX, 'is_taxable', ['required' => true, 'label' => 'Is Taxable']);
        $this->add(self::DROPDOWN, 'brand', ['required' => true, 'label' => 'Select Your Brand', 'placeHolder' => 'Select Brand', 'choices' => ['Brand A' => 1, 'Brand B' => 2, 'Brand C' => 3]]);
        $this->add(self::FILE, 'file_path', ['path' => 'myFolder', 'required' => true, 'label' => 'Upload your File']);
        return $this;
    }
}
  • In the above code, The 'name', 'description', 'is_active' should same name as data base table column names

  • Product Table

name description is_active is_new_arrival

Step 2 - Create Controller

Create a controller in app\Http\Controllers\ProductController.php

<?php

namespace App\Http\Controllers;

use App\Forms\ProductForm;
use App\Models\Product;
use Illuminate\Http\Request;
use Masuresh124\SimpleCrudBuilder\FormBuilder\FormBuilder;

class ProductController extends Controller
{

    public function index()
    {
        $data = Product::all();
        return view('product-list', compact('data'));
    }

    public function create()
    {
        $entity = new Product();
        return $this->process($entity);
    }

    public function store(Request $request)
    {
        $entity = new Product();
        return $this->process($entity);
    }

    public function edit(Product $product)
    {
        return $this->process($product);
    }
    public function update(Request $request, Product $product)
    {
        return $this->process($product);
    }

     /**
     *  This common function handles all the actions 
     *
     * @param  Product Model $entity
     * @return \Illuminate\Http\Response
     */
    public function process(Product $entity)
    {     /**
         * @param  Product Model $entity
         * @param  ProductForm::class 
         * @param  Form blade name  'product'
         */
        return FormBuilder::createFormSimpleBuilder($entity, ProductForm::class, 'product');
    }
}

Step 3 - Create Form Blade

Create a blade in \resources\views\product.blade.php

 <x-simple-crud-builder::form name="reseller-form" method="{{ $entity->method }}" action="{{ $entity->action }}" file="true">
                <div class="mb-3">
                    {{ $form->text('name') }}
                </div>
                <div class="mb-3">
                    {{ $form->textArea('description') }}
                </div>
                <div class="mb-3">
                    {{ $form->radio('is_active') }}
                </div>
                <div class="mb-3">
                    {{ $form->checkBox('is_new_arrival') }}
                </div>
                <div class="mb-3">
                    {{ $form->checkBox('is_best_seller') }}
                </div>
                <div class="mb-3">
                    {{ $form->checkBox('is_taxable') }}
                </div>
                <div class="mb-3">
                    {{ $form->dropDown('brand') }}
                </div>
                <div class="mb-3">
                    {{ $form->file('file_path') }}
                </div>
                <div class="m
                <div class="bottom-btn text-right mt-4">
                    <button type="submit" class="btn btn-primary">Save</button>
                </div>
            </x-simple-crud-builder::form>

Step 4 - Create list Blade

Create a blade to list products in \resources\views\product-list.blade.php

      <table class="table">
                <thead>
                    <tr>
                        <th scope="col">#</th>
                        <th scope="col">Name</th>
                        <th scope="col">Action</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($data as $key => $row)
                        <tr>
                            <th scope="row">{{ $key + 1 }}</th>
                            <td>{{ $row->name }}</td>
                            <td><a href="{{ route('products.edit', ['product' => $row->id]) }}">Edit</a></td>

                        </tr>
                    @endforeach
                </tbody>
            </table>

            <div class="bottom-btn text-right mt-4">
                <a href="{{ route('products.create') }}" class="btn btn-primary">Add</a>
            </div>

Step 5 - Create Route

Create a route in routes\web.php

Route::resource('products', ProductController::class);

Run the following command to access the uploaded file from the local

        php artisan storage:link

Badges

MIT License

Authors

Features

  • Relation form saving
  • Sub Form saving
  • Validations
  • Event handlers

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固