2

I have blade:

<div class="form-group">
                  <label for="exampleInputEmail1">Họ và tên</label>
                  <input type="text" class="form-control" id="exampleInputEmail1" placeholder="Nhập tên" name="username" value="{{old('username')}}">
                </div>
                <div class="form-group">
                  <label for="exampleInputEmail1">Email</label>
                  <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Nhập địa chỉ email" name="email" value="{{old('email')}}">
                </div>
                <div class="form-group">
                  <label for="exampleInputPassword1">Mật khẩu</label>
                  <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Nhập mật khẩu" name="pass1">
                </div>
                <div class="form-group">
                  <label for="exampleInputPassword1">Nhập lại mật khẩu</label>
                  <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Nhập lại mật khẩu" name="pass2">
                </div>
                <div class="checkbox">
                  <label>
                    <input type="checkbox" name="cbadmin" value="1" {{old('cbadmin') ? "checked" : ''}} > Admin
                  </label>
                </div>

I want set value variable $lv =1 while checkbox "cbadmin" checked. But not know how to retrieve it. I want set default lv=0, lv=1 if checkbox checked.

public function getAdd(AddUserRequest $request)
    {
        User::create([
            'name' => $request->username,
            'email' => $request->email,
            'password' => Hash::make($request->pass1),
            'level' => ????
        ]);
        return redirect('admin/manage-user/add')->with('success','Bạn đã thêm thành công!');
    }

2 Answers 2

2

You could check whether cbadmin exists in the $request. If it exists, it was checked and then you could set $lv to 1.

$lv=isset($request['cbadmin'])?1:0;
Sign up to request clarification or add additional context in comments.

1 Comment

Done. Thank you. I even forget set fillable column "level" in Model User.
0

Use input() with a default value for the case of cbadmin being unchecked:

User::create([
    'name' => $request->username,
    'email' => $request->email,
    'password' => Hash::make($request->pass1),
    'level' => (int)$request->input('cbadmin', 0)
 );

1 Comment

Done. Thank you. I even forget set fillable column "level" in Model User.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.