firstOrCreate()不插入和更新

时间:2018-07-01 19:32:23

标签: php database laravel eloquent

如果表列不存在,我想尝试更新表列,然后创建新列,然后更新此行。但是它像下面这样向我发送此错误:

  

SQLSTATE [HY000]:常规错误:1364字段“ option_value”没有   具有默认值(SQL:插入adminsoption_name中,   updated_atcreated_at)值(purchase_delivery,2018年7月1日   19:31:31,2018-07-01 19:31:31))

public function update(Request $request)
{
    //
    //dd($request->input());

    $admin = Admin::firstOrCreate(['option_name' => 'tag']);
    $admin->option_value=$request->tag;
    $admin->save();
    $admin = Admin::firstOrCreate(['option_name' => 'review']);
    $admin->option_value=$request->review;
    $admin->save();
    $admin = Admin::firstOrCreate(['option_name' => 'purchase_delivery']);
    $admin->option_value=$request->purchase_delivery;
    $admin->save();
    $admin = Admin::firstOrCreate(['option_name' => 'replace_policy']);
    $admin->option_value=$request->replace_policy;
    $admin->save();
    return redirect()->route('admin.settings')
        ->with('success', 'Setting saved successfully');
}

HTML代码:

                        <div class="col-sm-4">
                            <select name="tag" style="margin-bottom:15px;" class="form-control">
                                <option value="1" {{$tag !=null ?($tag->first()->option_value==1 ?'selected':''):('')}}>On</option>
                                <option value="0" {{$tag !=null ?($tag->first()->option_value==0 ?'selected':''):('')}}>Off</option>
                            </select>
                        </div>
                        <div class="col-sm-2">
                            <label class="control-label">Review System</label>
                        </div>

                        <div class="col-sm-4">
                            <select name="review" style="margin-bottom:15px;" class="form-control">
                                <option value="1" {{$review !=null ?($review->first()->option_value==1 ?'selected':''):('')}}>On</option>
                                <option value="0" {{$review !=null ?($review->first()->option_value==0 ?'selected':''):('')}}>Off</option>
                            </select>
                        </div>
<textarea name="purchase_delivery" id="summernote_1">{!!$purchase_delivery !=null ? $purchase_delivery->first()->option_value :''!!}</textarea>
<textarea name="replace_policy" id="summernote_2">{!!$replace_policy !=null ? $replace_policy->first()->option_value :''!!}</textarea>

1 个答案:

答案 0 :(得分:3)

该字段library(tidyverse) df1 %>% distinct(User_Name) %>% pull(User_Name) %>% map_df(~ df1 %>% filter(User_Name == .x) %>% mutate(Flag = toString(setdiff(unique(df1$Group), unique(Group)))) %>% slice(1) %>% select(-Group)) %>% right_join(df1, "User_Name") 需要一个值。

您仅在执行以下操作时为option_value设置一个值:

option_name

您需要为Admin::firstOrCreate(['option_name' => 'replace_policy']); 传递一些值:

option_value

第一个参数是用于构建WHERE查询的参数。第二个参数是创建模型时要使用的值(它们与WHERE数组合并)。这要求这两个字段都必须填写。

OR

如果在创建时确实没有“拥有”值,那么请在模式中添加该字段Admin::firstOrCreate( ['option_name' => 'replace_policy'], ['option_value' => 'some value'] );