Laravel二级联动

Laravel二级联动,选择大类,初始化相应的小类。
路由:routes.php

/**
 *  材料品类管理--面料
 */
$router->group(['prefix' => 'fam'], function ($router) {
    $router->get('', FamController::class.'@index');              //列表
    $router->get('create', FamController::class.'@create');       //新增页
    $router->post('', FamController::class.'@store');             //提交
    $router->get('{id}/edit', FamController::class.'@edit');      //编辑页
    $router->put('{id}', FamController::class.'@update');         //修改
    $router->get('{id}/detail', FamController::class.'@detail');  //查看
    $router->get('api', FamController::class.'@api');  //查看
});

控制器:FamController.php

在这里插入图片描述
在这里插入图片描述

<?php

namespace App\Admin\Controllers;

use App\Models\Fam;
use App\Models\MaterialCategory;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FamController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '面料';
    protected $description = [
        'index'  => '列表',
        'edit'   => '编辑',
        'create' => '新增',
    ];

    public function index(Content $content)
    {
        $content->breadcrumb(
            ['text' => $this->title, 'url' => '/fam']
        );
        return parent::index($content); // TODO: Change the autogenerated stub
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Fam);
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->disableColumnSelector(); //屏蔽字段筛选
        $grid->disableRowSelector();
        //$grid->column('id', __('ID'));
        $grid->column('fam_no', __('编号'));
        $grid->column('getBigSelectOptions.title', __('归属大类'));
        $grid->column('getSmallSelectOptions.title',__('归属小类'));
        $grid->column('name', __('材料名称'));
        $grid->column('meaunit_name',__('单位'))->display(function (){
            return pub_meaunit()[$this->meaunit];
        });
        $grid->tools(function ($tools){
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
            $tools->append('<a href="/admin/fam/create" class="btn btn-sm btn-success" title="新增">
                        <i class="fa fa-plus"></i><span class="hidden-xs">&nbsp;&nbsp;新增</span>
                    </a>');
        });

        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->disableDelete();
            $actions->disableEdit();        //屏蔽修改
            $actions->append('<a href="/admin/fam/'.$actions->getkey().'/edit" class="btn btn-sm btn-default" >修改</a>');
        });

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();

            $filter->column(1/3, function ($filter) {
//                $filter->equal('bigclass','所属大类')->select(MaterialCategory::pluck('title', 'id'));
                $filter->equal('bigclass','所属大类')->select(MaterialCategory::getSelectOptions(['parent_id'=>8]))->load('smallclass','/admin/fam/api');
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('smallclass','所属小类')->select(MaterialCategory::all()->pluck('title', 'id'));
            });
            $filter->column(1/3, function ($filter) {
                $filter->like('name', '材料名称');
            });
        });
        $grid->model()->where('flag','0');
        return $grid;
    }

    public function create(Content $content)
    {
        $content->breadcrumb(
            ['text' => $this->title, 'url' => '/fam'],
            ['text' => '新增']
        )
        ->row($this->form());
        return $content;
//        return parent::create($content); // TODO: Change the autogenerated stub
    }

    public function edit($id, Content $content)
    {
        $content->breadcrumb(
            ['text' => $this->title, 'url' => '/fam'],
            ['text' => '编辑']
        );
        return parent::edit($id, $content); // TODO: Change the autogenerated stub
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Fam());
        $form->select('bigclass','所属大类')->options(MaterialCategory::getSelectOptions(['parent_id'=>8]))->rules('required')->load('smallclass','/admin/fam/api');

        //\request()->bigclass; //外取bigclass
       // dump(\request()->bigclass);
        /**
         * 选择大类 初始化小类
         */
        $form->select('smallclass','所属小类')->rules('required');
        $form->text('fam_no','编号')->rules('required|regex:/^\d+$/');
        $form->text('name', __('材料名称'))->rules('required|max:50');
        $form->select('meaunit','单位')->options(pub_meaunit());
        $form->saving(function (Form $form) {
            //dd($form->bigclass);
            $form->model()->user_id = Admin::user()->id;
            $form->model()->flag = 0;
        });
        $form->tools(function ($tools){
            $tools->disableView();
            $tools->disableDelete();
        });

        $form->footer(function ($footer) {
            $footer->disableReset();
          //$footer->disableSubmit();
            $footer->disableSaves();
        });
        return $form;
    }

    public function api (Request $request)  
    {
        $id = $request->get('q');
        return MaterialCategory::where(['parent_id'=>$id])->get(['id', DB::raw('title as text')]);
    }

}

模型Fam.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Fam extends Model
{
    protected $table = 'pub_fam_info';

    protected $fillable = ['fam_no','name','bigclass','smallclass','meaunit','flag','status','user_id'];

    public function admin_info()
    {
        return $this->belongsTo(Admin_users::class, 'user_id', 'id');
    }

    //品类:大类
    public function getBigSelectOptions()
    {
        return $this->belongsTo(MaterialCategory::class, 'bigclass', 'id');
    }

    //品类:小类
    public function getSmallSelectOptions()
    {
        return $this->belongsTo(MaterialCategory::class, 'smallclass', 'id');
    }

    public static function getUnitOptions()
    {
        $options = DB::table('pub_meaunit')->select('pub_meaunit.id','pub_meaunit.name')->get();
        $selectOption=[];
        foreach ($options as $option) {
            $selectOption[$option->id] = $option->name;
        }
        return $selectOption;
    }

    public static function getSelectOptions($where)
    {
        $options = DB::table('pub_fam_info')->where($where)->select('id', 'bigclass' , 'smallclass' ,'name','meaunit')->get();
        $selectOption = [];
        foreach ($options as $option) {
            $selectOption[$option->id] = $option->title;
        }
        return $selectOption;
    }

}
发布了34 篇原创文章 · 获赞 2 · 访问量 910

猜你喜欢

转载自blog.csdn.net/xcbzsy/article/details/103313261