Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可以修改提交表单时的逻辑吗 #375

Closed
fengshifen opened this issue Feb 6, 2017 · 17 comments
Closed

可以修改提交表单时的逻辑吗 #375

fengshifen opened this issue Feb 6, 2017 · 17 comments

Comments

@fengshifen
Copy link

因为现在提交表单时,都是框架直接写好了。但有时提交表单时,逻辑可能要自己重写

@z-song
Copy link
Owner

z-song commented Feb 6, 2017

举个例子?

@fengshifen
Copy link
Author

比如提交一个表单时,表单的数据可能不是只保存到一个数据表中,可能还要保存到其他关联的表中。

@z-song
Copy link
Owner

z-song commented Feb 6, 2017

类似这种附加的数据库操作,可以在模型的保存回调里面完成

模型里面添加代码:

public static function boot()
{
    parent::boot();

    static::saving(function ($model) {

        // 把数据从$model取出,插入到其它表中

    });
}

@fengshifen
Copy link
Author

fengshifen commented Feb 6, 2017

新增和修改操作的代码都是写在下面的saving方法里面吗,还有$model参数可以获取到表单的数据吗

public static function boot()
{
    parent::boot();

    static::saving(function ($model) {

        // 把数据从$model取出,插入到其它表中

    });
}

@z-song
Copy link
Owner

z-song commented Feb 6, 2017

updating creating saving 这几个方法你自己选择,打印一下$model看看你就知道怎么取出数据了

@fengshifen
Copy link
Author

还有,$form->multipleSelect('tags')->options(Tag::all()->pluck('name', 'id')); 好像$model获取不到多选框的值

@z-song
Copy link
Owner

z-song commented Feb 6, 2017

$model获取不到 就用Request对象获取

@fengshifen
Copy link
Author

fengshifen commented Feb 6, 2017

image

$id = DB::table('recipes')->insertGetId([
    'author_id' => 1,
    'title' => Input::get('title'),
    'quantity' => Input::get('quantity'),
    'duration' => Input::get('duration'),
    'difficulty' => Input::get('difficulty'),
    'introduction' => Input::get('introduction'),
]);

提交表单时,数据表里面的非空字段必须要出现在代码里面吗?比如我上面的代码没有加author_name字段就会报错

@z-song
Copy link
Owner

z-song commented Feb 6, 2017

是的,你要调整一下

@z-song z-song closed this as completed Feb 6, 2017
@chenzi2015
Copy link

感觉这样处理逻辑只能处理很简单的逻辑, 很复杂的逻辑还是需要在控制器里面写。

@dullme
Copy link

dullme commented Jul 20, 2018

如果我要保存密码是不是也要这么做

public static function boot()
    {
        parent::boot();

        static::saving(function ($model) {
            $model->password = bcrypt($model->password);
        });
    }

@lyongdee
Copy link

lyongdee commented Nov 15, 2018

@z-song 表单里有一些元素,提交的时候要分别写到几张数据表里,因为有很多表单元素分别写到几张数据 表,请教下哪种更合适逻辑处理更简单呢,1、这个也是用当前模型的 boot 方法在使用 saving()、creating()、saving() 里处理吗?2、可不可以在模型表单 Form 里直接处理?

@lepig
Copy link
Contributor

lepig commented Nov 30, 2018

@lyongdee 老铁 你是咋处理的。我也遇到这个问题。一个表单里的数据需要保存到3张表,按照文档上的

$form->multipleSelect('channels.fr', '渠道来源')->options([
                 'foo' => 'fooo',
                 'bar' => 'barr',
                 'haha' => 'HAHA'
             ]);

没插入成功。
channels是我定义在model里的方法,fr是关联表中的字段

@Wadaniu
Copy link

Wadaniu commented Aug 12, 2020

类似这种附加的数据库操作,可以在模型的保存回调里面完成

模型里面添加代码:

public static function boot()
{
    parent::boot();

    static::saving(function ($model) {

        // 把数据从$model取出,插入到其它表中

    });
}

使用这种方式处理后form的input中依然有这个字段对应的参数,在插入时则会报Unknown column 'xxx' in 'field list'的错误,应该如何解决呢,usnet($model->xxx)无法解决

@Wdfwilson
Copy link

Wdfwilson commented Jan 15, 2021

saving前用
$form->ignore(['xxx','yyy']);

@echoWangyx
Copy link

类似这种附加的数据库操作,可以在模型的保存回调里面完成

模型里面添加代码:

public static function boot()
{
    parent::boot();

    static::saving(function ($model) {

        // 把数据从$model取出,插入到其它表中

    });
}

大佬,请问回调中有异常如何回滚表单呢。我现在只能回滚我自己回调的代码,但是表单本身的数据还是提交成功了

@Shuixiang
Copy link

有一个场景,比如保存模型的时候,如果有某个属性提交过来是空的,那么将不更新这个属性,在saving回调,有办法在空值的时候去掉吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants