0

I have this form group:

<select class="form-control" id="sel1" name="status">
   @if((auth()->user()->two_factor_auth) === 'off')
   <option value="off">Off</option>
   <option value="sms">SMS</option>
   @else
   <option value="off">Off</option>
   <option value="sms">SMS</option>
   @endif
</select>

And as the action, I added this to the method submit of ProfileController:

public function submit(Request $request)
{
    if($data['status'] === 'off'){
        $request->user()->update([
            'two_factor_auth' => 'off'
        ]);
    }else{...}
}

And then at the DB,users table, I have added two_factor_auth column and it is set to sms by default.

So if the user selects, off, it should update the column and set off. But now the problem is it does not do that somehow...

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.

2
  • 2
    Sidenote, you have the same <option>s in the if and else statements, which means as written, it is currently pointless. Is one of those supposed to be on? You can also move the SMS option out of that @if so you don't have to write it twice. Commented Jun 1, 2021 at 13:46
  • 2
    Next, $data['status'] is not how you access that; it's $request->input('status'). Currently, $data is not defined in the code you posted. Commented Jun 1, 2021 at 13:47

1 Answer 1

1

Your html need to be replaced, add the selected attribute to the option you find in your DB/object

<select class="form-control" id="sel1" name="status">
   @if((auth()->user()->two_factor_auth) === 'off')
   <option selected value="off">Off</option>
   <option value="sms">SMS</option>
   @else
   <option value="off">Off</option>
   <option selected value="sms">SMS</option>
   @endif
</select>

You can also do in in just two lines

@php
$isOff = auth()->user()->two_factor_auth === 'off';
@endphp
<select class="form-control" id="sel1" name="status">
   <option @if($isOff) selected @endif value="off">Off</option>
   <option @if(!$isOff) selected @endif value="sms">SMS</option>
</select>

As for the controller, maybe you did not post all the code, but try this

public function submit(Request $request)
{
    //you should add validation rules here
    $user = auth()->user();
    $user->status = $request->input('status', 'sms');//second attribute is for default
    //you can add other attributes to update here 
    $user->save();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Alternative syntax; I find <option {{ $isOff ? 'selected' : '' }}> or <option {{ !isOff ? 'selected' : '' }}> cleaner than an inline @if ... @endif (both work just fine though)
@TimLewis it's does the same thing but using @if removes the else statement.
Ah yeah, fair enough point; and the : '' portion of the ternary serves no purpose here. Like I said, either or is fine :)

Your Answer

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