-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Comments
举个例子? |
比如提交一个表单时,表单的数据可能不是只保存到一个数据表中,可能还要保存到其他关联的表中。 |
类似这种附加的数据库操作,可以在模型的保存回调里面完成 模型里面添加代码: public static function boot()
{
parent::boot();
static::saving(function ($model) {
// 把数据从$model取出,插入到其它表中
});
} |
新增和修改操作的代码都是写在下面的saving方法里面吗,还有$model参数可以获取到表单的数据吗 public static function boot()
{
parent::boot();
static::saving(function ($model) {
// 把数据从$model取出,插入到其它表中
});
} |
|
还有,$form->multipleSelect('tags')->options(Tag::all()->pluck('name', 'id')); 好像$model获取不到多选框的值 |
|
$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 表单里有一些元素,提交的时候要分别写到几张数据表里,因为有很多表单元素分别写到几张数据 表,请教下哪种更合适逻辑处理更简单呢,1、这个也是用当前模型的 boot 方法在使用 saving()、creating()、saving() 里处理吗?2、可不可以在模型表单 Form 里直接处理? |
@lyongdee 老铁 你是咋处理的。我也遇到这个问题。一个表单里的数据需要保存到3张表,按照文档上的
没插入成功。 |
使用这种方式处理后form的input中依然有这个字段对应的参数,在插入时则会报Unknown column 'xxx' in 'field list'的错误,应该如何解决呢,usnet($model->xxx)无法解决 |
saving前用 |
大佬,请问回调中有异常如何回滚表单呢。我现在只能回滚我自己回调的代码,但是表单本身的数据还是提交成功了 |
有一个场景,比如保存模型的时候,如果有某个属性提交过来是空的,那么将不更新这个属性,在saving回调,有办法在空值的时候去掉吗 |
因为现在提交表单时,都是框架直接写好了。但有时提交表单时,逻辑可能要自己重写
The text was updated successfully, but these errors were encountered: