skip to Main Content

Database table Column type is Boolean and nullable.

Value inserting to value is true or false:

$statusVal = $request->input('status');

if($statusVal == "yes"){
    $toInsert = true;
}else if($statusVal == "no"){
    $toInsert = false;
}

ServiceDetail::create(['status' => $toInsert]);

I want to insert null to $toInsert, if status is not selected

Please guide me through this

2

Answers


  1. $statusVal = $request->input('status');
    
    if($statusVal == "yes"){
        $toInsert = true;
    }
    
    if($statusVal == "no"){
        $toInsert = false;
    }
    
    if(is_null($statusVal)){
        $toInsert = null;
    }
    
    Login or Signup to reply.
  2. You can do it with a single statement using nested-ternary-operator.

    Just do it.

    $statusVal = $request->input('status');
    
    $toInsert = empty($statusVal) ? null 
                : ($statusVal == 'yes' ? true : false);
    

    Try this, it will work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search