laravel 11使用OwlAdmin插入可视化代码后如何提交表单

<?php

namespace App\Admin\Controllers;

use Illuminate\Http\Request;
use Slowlyo\OwlAdmin\Renderers\Tab;
use Slowlyo\OwlAdmin\Renderers\Tabs;
use Slowlyo\OwlAdmin\Renderers\Alert;
use Slowlyo\OwlAdmin\Renderers\TextControl;
use Slowlyo\OwlAdmin\Controllers\AdminController;
use App\Services\AdminSettingService;

class SettingCommonController extends AdminController
{
    protected string $serviceName = AdminSettingService::class;

    public function index()
    {
        $schema = $this->form();

        return $this->response()->success($schema);
    }

    public function form()
    {
        $test = amis()->Page()->regions([
            '0' => 'body',
        ])->id('u:ac9313ae40b6')->pullRefresh([
            'disabled' => '1',
        ])
        ->body([
                amis()->Form()->id('u:7e594dc48d01')->title('')->mode('flex')->labelAlign('left')->dsType('api')->feat('Insert')->body([
                        amis()->TextControl()->name('Common_SiteName')->label('站点名称')->row('0')->id('u:34678a1d1cda')->colSize('1/4'),
                        amis()->TextControl()->name('Common_SiteURL')->label('站点地址')->row('1')->id('u:8451804df54e')->colSize('1/4'),
                        amis()->TextControl()->name('Common_SiteEML')->label('站点地址')->row('2')->id('u:70cbdf798792')->colSize('1/4'),
                        amis()->TextControl()->name('Common_KeyWords')->label('KeyWords')->row('3')->id('u:5dd322fc4542')->colSize('1/2'),
                        amis()->TextControl()->name('Common_Description')->label('Description')->row('4')->id('u:f81887c6cb76')->colSize('1/2'),
                        amis()->TextControl()->name('Common_IPC')->label('网站备案信息代码')->row('5')->id('u:f86dc1a7be74')->colSize('1/2'),
                ])->actions([
                        amis()->VanillaAction()->label('提交')->onEvent([
                            'click' => [
                                'actions' => [
                                    [
                                        'actionType' => 'submit',
                                        'componentId' => 'u:7e594dc48d01',
                                    ],
                                ],
                            ],
                        ])->level('primary')->id('u:55992430ba60'),
                ])->resetAfterSubmit('1')->wrapWithPanel('1')->labelWidth('12.5rem'),
        ]);

         return amis()->Page()->regions([
            '0' => 'body',
        ])->id('u:04046f014e58')->pullRefresh([
            'disabled' => '1',
        ])->body([
                amis()->Tabs()->tabs([
                    [
                        'title' => '站点信息',
                        'body' => $test,
                        'id' => 'u:0852ef9d24e4',
                    ],
                    [
                        'title' => '时间设置',
                        'body' => [123],
                        'id' => 'u:6aedd1e4dfdb',
                    ],
                ])->mountOnEnter('1')->id('u:eb024e6c3270')->tabsMode('strong')->showTip(''),
        ]); 
    }

    public function store(Request $request)
    {
        $data = $request->only([
            'Common_SiteName',
            'Common_SiteURL',
            'Common_SiteEML',
            'Common_KeyWords',
            'Common_Description',
            'Common_IPC',
        ]);

        return settings()->adminSetMany($data);
    }
}

我给每一个下拉列表设定一个控制器,然后每个下拉列表中又同时存在多个tabs选项卡。所以目前写法是这样的。

然后创建好了models,

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Slowlyo\OwlAdmin\Models\BaseModel as Model;

/**
 * pre_admin_settings
 */
class AdminSetting extends Model
{
    use SoftDeletes;

    protected $table = 'admin_settings';
}

和 services

<?php

namespace App\Services;

use App\Models\AdminSetting;
use Slowlyo\OwlAdmin\Services\AdminService;

/**
 * pre_admin_settings
 *
 * @method AdminSetting getModel()
 * @method AdminSetting|\Illuminate\Database\Query\Builder query()
 */
class AdminSettingService extends AdminService
{
    protected string $modelName = AdminSetting::class;
}

那么 我再OwlAdmin 中 如何把表单提交到数据库呢?

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

// 在控制器中 use App\Services\AdminSettingService;

public function updateSettings(Request $request) { // 验证请求数据(如果需要) $validatedData = $request->validate([ // 添加验证规则 ]);

// 使用服务层来处理业务逻辑
$service = new AdminSettingService();
$result = $service->updateSettings($validatedData);

if ($result) {
    // 处理成功逻辑,比如重定向到成功页面
    return redirect()->back()->with('success', '设置已更新');
} else {
    // 处理失败逻辑,比如重定向回表单页面并带上错误信息
    return redirect()->back()->with('error', '更新失败');
}

}

5个月前 评论
讨论数量: 2
slowlyo

需要给 form 配置 api 属性, amis 可视化编辑器生成的页面只是前端页面, 保存逻辑需要自己实现

5个月前 评论

// 在控制器中 use App\Services\AdminSettingService;

public function updateSettings(Request $request) { // 验证请求数据(如果需要) $validatedData = $request->validate([ // 添加验证规则 ]);

// 使用服务层来处理业务逻辑
$service = new AdminSettingService();
$result = $service->updateSettings($validatedData);

if ($result) {
    // 处理成功逻辑,比如重定向到成功页面
    return redirect()->back()->with('success', '设置已更新');
} else {
    // 处理失败逻辑,比如重定向回表单页面并带上错误信息
    return redirect()->back()->with('error', '更新失败');
}

}

5个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!