Uzzal Hosen
Uzzal Hosen

Reputation: 353

firstOrCreate() not inserting and updating

I want trying updating my table column if table column not exist then i want to create new column and then update this row. But it send me this Error like this bellow:

SQLSTATE[HY000]: General error: 1364 Field 'option_value' doesn't have a default value (SQL: insert into admins (option_name, updated_at, created_at) values (purchase_delivery, 2018-07-01 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 Code:

                        <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>

Upvotes: 0

Views: 1734

Answers (1)

lagbox
lagbox

Reputation: 50491

That field, option_value, needs a value.

You are only setting a value for option_name when doing this:

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

You will need to pass some value for option_value:

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

The first argument is what it will use to build the WHERE query. The second argument are values to use when creating the model (they get merged with the WHERE array). This requires both those fields to be fillable.

OR

Make that field nullable in the schema, if it is really something that doesn't "have" to have a value when created.

Upvotes: 3

Related Questions