0
votes

I'm working with Laravel 8 to develop my project and in this project, I have made a page for adding users. And I have a also added a form field of status which determines if the user has actived it's account or not:

<select class="form-control" name="status">
   <option value="1">Active</option>
   <option value="0">Deactive</option>
</select>

And at the Controller, I've added this:

public function store(Request $request)
{
    $validate_data = Validator::make(request()->all(),[
        'name' => 'required|min:4',
        'email' => 'required|email|unique:users',
        'status' => 'required',
        'password' => 'required|min:6'
    ])->validated();

    $creation = User::create([
        'name' => $validate_data['name'],
        'email' => $validate_data['email'],
        'status' => $validate_data['status'],
        'password' => $validate_data['password']
    ]);

    if(!$creation){
        return '1003';
    }
}

But now I get this error:

Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1364 Field 'status' doesn't have a default value (SQL: insert into users (name, email, password, updated_at, created_at) values (somename, someemail, somepassword, 2021-05-09 06:59:44, 2021-05-09 06:59:44))

So what is going wrong here ? How can I fix this issue ?

I would really appreciate any idea or suggestion from you guys...

Thanks in advance.

1

1 Answers

0
votes

You need to put status to fillable array of your model, or add guarded = []; to your model.

User model

User
{
    $fillable = [
        // Add status here with other field
    ];

}