laravel-admin 自定义form 系统配置示例

卞浩漫
2023-12-01

迁移文件

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateConfigTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('config', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('k');
            $table->string('v');
            $table->string('text')->comment('备注');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('config');
    }
}

model

<?php
namespace App\Models\Admin;
use Illuminate\Database\Eloquent\Model;

/**
 * Created by PhpStorm.
 * User: EricPan
 * Date: 2020/5/20
 * Time: 10:32
 */

class Config extends Model
{
    protected $table = 'config';

    /**
     * 查询数据
     * Created by PhpStorm.
     * User: EricPan
     * Date: 2020/5/20
     * Time: 10:49
     * @param $in
     * @return \Illuminate\Support\Collection
     */
    public static function getWhereIn($in)
    {
        return Config::query()->whereIn('id',$in)->get()->pluck('v','k');
    }


    /**
     * 修改
     * Created by PhpStorm.
     * User: EricPan
     * Date: 2020/5/20
     * Time: 10:51
     * @param $k
     * @param $v
     * @return int
     */
    public static function up($k,$v)
    {
        return Config::query()->where('k',$k)->update([
            'v' =>$v
        ]);
    }
}

页面

/**
     * 系统设置
     * Created by PhpStorm.
     * User: EricPan
     * Date: 2020/5/28
     * Time: 10:20
     * @param Content $content
     * @return Content
     */
    public function systemSet(Content $content)
    {
        $forms = [
            'prize' => SystemSetting::class
        ];

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

Form页面

<?php

namespace App\Admin\Forms;

use App\Models\Admin\Config;
use App\Repository\Common\commonRepository;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

class SystemSetting extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '抽奖设置';

    /**
     * Created by PhpStorm.
     * User: EricPan
     * Date: 2020/5/29
     * Time: 11:15
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|mixed
     * @throws \App\Exceptions\CommonException
     */
    public function handle(Request $request)
    {

        $data['prize_img'] = $this->imgAli($request,'prize_img');
        $data['prize_prestige'] = $request->input('prize_prestige');

        if(count($data))
        {
            foreach ($data as $k=>$v)
            {
                if($v)
                {
                    Config::up($k,$v);
                }

            }
        }

        admin_toastr('设置成功');

        return back();
    }

    /**
     * 图片上传阿里云
     * Created by PhpStorm.
     * User: EricPan
     * Date: 2020/5/29
     * Time: 11:13
     * @param Request $request
     * @param $key
     * @return mixed|string
     * @throws \App\Exceptions\CommonException
     */
    public function imgAli(Request $request,$key)
    {
        $path = $request->input($key);
        $prize_img = $request->file($key);
        if($prize_img)
        {
            $common = new commonRepository();
            $re = $common->img_upload($request,1,$key);
            $path = arrIsKey($re,'path');
        }
        return $path;
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->text('prize_prestige','抽奖声望')->rules('required')->help('每次抽奖扣除声望值');
        $this->image('prize_img','抽奖图片')->uniqueName();
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        $data = Config::getWhereIn([1,2]);
        return [
            'prize_prestige'       => arrIsKey($data,'prize_prestige'),
            'prize_img' => arrIsKey($data,'prize_img'),
        ];
    }
}

 类似资料: