skip to Main Content

so I have a form with a select option inside it, and I’m trying to get the choice of the user and then insert it into the table and then database, but it doesn’t seem to be working. Here is the code I thought was necessary to understand the problem, if there is anything else you need please let me know!

HTML

<form method="POST" action="{{ route('createEvent') }}">
    <label for="is_private">Privacy</label>
    <select id="is_private" name="is_private" required autofocus>
        <option value="" disabled selected>-- select one --</option>
        <option value="is_public">Public</option>
        <option value="is_private">Private</option>
    </select>
</form>

EventController.php

if (null !== $request->input('is_private')) {
  $event->is_private = 'true';
}
  else {
    $event->is_private = 'false';
}

The problem is that it’s always returning with $event->is_private = 'true'; and I don’t know why…

Thanks alot for any help you may provide!

3

Answers


  1. You need to use following code. I think you want that if user select is_private option then $event->is_private is true other wise false.

    if ($request->input('is_private') == "is_private") {
      $event->is_private = 'true';
    }
    else {
        $event->is_private = 'false';
    }
    
    Login or Signup to reply.
  2. Hmm.. I would try something like:

    switch( $request->input( 'is_private' ) )
    {
       case 'is_private':
         $event->is_private = 'true';
         break;
    
       default:
         $event->is_private = 'false';
         break;
    }
    
    Login or Signup to reply.
  3. $event->is_private = ($request->post(‘is_private’) === ‘is_private’);
    

    That should do it in one line and be an actual Boolean type.

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