lavavel-admin 动态添加表单组

暂无标签

以主导航为例,保存到数据库并添加缓存到interfaces表(字段:name,value)
场景:后台界面设置tab页面之主导航设置。

路由

$router->resource('/interfaces', InterFacesController::class);

控制器

/app/Admin/Controllers/InterFacesController.php

<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;

class InterFacesController extends Controller
{
    public function index(Content $content)
    {
        $forms = [
            'menu'=> InterFaces\Menu::class,
        ];
        
        $content->breadcrumb(['text' => '界面', 'url' => '/interfaces/base']);

        return $content
            ->title('界面设置')
            ->body(Tab::forms($forms));
    }
}

模型

/app/Admin/Models/InterFaces.php

<?php

namespace App\Admin\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Artisan;
use Cache;

class InterFaces extends Model
{
    protected $table = 'interfaces';
    protected $primaryKey = 'name';
    public $incrementing = false; // 防止设主键为name致获值为0
    protected $fillable = ['name', 'value'];
    public $timestamps = false;

    public static function boot()
    {
        //
        parent::boot();
    }
    
    /* 根据interfaces表更新缓存 */
    public static function setcache()
    {
        Cache::forever('interfaces',(object)array_column(self::all()->toArray(),'value','name'));
    }
    
    /* 设置配置信息 */
    public static function set($name , $value)
    {
            $data = [];
            foreach($value as $v){
                if($v['_remove_'] == '0'){
                    $data[] = $v;
                }
            }
        self::updateOrCreate(['name'=>$name],['value'=>json_encode(array_values($data))]);
    }
    
        /* 查询某个配置信息 */
    public static function get($name,$default='')
    {
        $data =  self::where('name','=',$name)->first();
        if($data){
            return $data->value;
        }
        return $default;
    }

}

tab页面

/app/Admin/Controllers/InterFaces/Menu.php

<?php

namespace App\Admin\Controllers\InterFaces;

use Illuminate\Support\Facades\Lang;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use App\Admin\Models\InterFaces;
use Cache;

class Menu extends Form
{

        public $title = '主导航';
        protected $casts = [
        'value' => 'json',
    ];
        
        public function handle(Request $request)
    {        
            $validateRules = [
            'name' => 'required|max:128',
        ];
        $this->validate($request,$validateRules);
        $data = $request->except('_token');
        
        foreach($data as $name=>$value){
                  InterFaces::set($name,$value);
        }

        InterFaces::setcache();

        admin_success('','保存成功');
        return back();
    }
    public function form()
    {
        $this->table('main_menu',__('主导航'), function ($table) {
                $table->number('order',__('排序'));
                    $table->text('title',__('名称'));
                    $table->text('url',__('链接地址'));
                    $states = ['on'  => ['value' => '1', 'text' => '显示', 'color' => 'success'],'off' => ['value' => '0', 'text' => '隐藏', 'color' => 'danger'],];
                    $table->switch('open',__('是否显示'))->states($states);
                });
    }
    public function data()
    {
        $menus = array_values(json_decode(InterFaces::get('main_menu'), true) ?: []);
            array_multisort(array_column($menus, 'order'), SORT_ASC, $menus);
        return [
            'main_menu'=> $menus,
        ];
    }
}

视图调用

因为是保存到cache,所以

@if(Cache::has('interfaces'))
    @php
    $menus = array_values(json_decode(Cache::get('interfaces')->main_menu, true) ?: []);
    array_multisort(array_column($menus, 'order'), SORT_ASC, $menus);
    @endphp
    @foreach($menus as $menu)
    @if($menu['open'] == 'on')
    <li><a href="{{ $menu['url'] }}" class="nav-link">{{ $menu['title'] }}</a></li>
    @endif
    @endforeach
    @else
    <li class="nav-link">{{ __('No Cache') }}</li>
@endif