简体   繁体   中英

Laravel 5.4 Field doesn't have a default value with no use of create

Very strange behavior with save() . I don't use method create . This is the creating new record code:

public function store(Request $request)
    {
        $this->validate($request,[
            'title' => 'required',
            'weight' => 'required|integer|max:99,min:-99',
            'color' => 'required|regex:/^#[0-9A-F]{6}$/',
            'suspension' => 'required|integer|in:0,1'
        ]);        
        $status = new Status();
        $status->weight = $request->weight;
        $status->title = request('title');
       // dd($request->title);

        $status->color = $request->color;
        $status->suspension = $request->suspension;
        $status->description = $request->description;
        $status->save();
        $request->session()->flash('status','success');
        $request->session()->flash('msg',__('Status has benn created Successfully'));
        return redirect('/status');

    }

However, I have got the following message about the title field:

SQLSTATE[HY000]: General error: 1364 Field 'title' doesn't have a default value (SQL: insert into statuses ( weight , color , suspension ) values (-9, #FFD700, 0))

So it is not an issue of $fillable !

I have the translation package Laravel Translatable . Just when stop using it from Status model, it saves fine!

However I have another model which uses the same package with the same way but it could able to save new records successfully!

I could not able to figure out the root cause f this issue.

Update

The only working solution till now, is to set 'strict' => false, in the MySQL configuration. However, I still does not able to understand what's happening?!

did you notice that you have used like this $status->title = request('title'); so change it to $status->title = $request->title;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM