【问题标题】:firstOrCreate() not inserting and updatingfirstOrCreate() 不插入和更新
【发布时间】:2018-07-01 19:32:23
【问题描述】:

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

SQLSTATE[HY000]:一般错误:1364 字段 'option_value' 没有 有一个默认值(SQL:插入adminsoption_nameupdated_at, created_at) 值(purchase_delivery,2018-07-01 2018-07-01 19:31:31 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>

【问题讨论】:

  • 您的option_value 也需要在查询中传递,需要设置为默认值,或者需要设置为允许为NULL。
  • 谢谢我加了$table-&gt;longText('option_value')-&gt;nullable();
  • 你能告诉我如何让我更有活力吗?

标签: php database laravel eloquent


【解决方案1】:

那个字段,option_value,需要一个值。

这样做时,您只是为option_name 设置了一个值:

Admin::firstOrCreate(['option_name' => 'replace_policy']);

您需要为option_value 传递一些值:

Admin::firstOrCreate(
    ['option_name' => 'replace_policy'],
    ['option_value' => 'some value']
);

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

在架构中创建该字段nullable,如果它确实是在创建时没有“必须”具有值的东西。

【讨论】:

    猜你喜欢
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 2012-08-19
    相关资源
    最近更新 更多