0

I have a few select-option with PHP inside of them, and I need to change this to input checkboxes. I'm using this for a composer message function, and I want to have more checkboxes instead of a select multiple option.

Here is my model:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Mailbox extends Model
{
    protected $table = 'mailbox';
    public $timestamps = false;
    protected $fillable = [
        'target_username', 'subject', 'message','date','sender_id','target_id'
    ];
}

Here is my controller:

public function composeMessage(Request $request)
{
    if ( $user = Sentinel::check() ) {
        // return $request->all();
        $data = $this->data;
        $id = $user->id;

        foreach ($request['target_id'] as $key => $target_id) {
            $user = User::findOrfail($id);
            $target = User::findOrfail($target_id);
            $mailbox = Mailbox::create([
                'sender_id' => $id,
                'target_username' => $target->username,
                'target_id' => $target->id,
                'subject' => $request['subject'],
                'message' => $request['message'],
                'date' => strtotime(date('Y-m-d')),
            ]);

            $log = \App\Log::create([
                'user_id' => $target_id,
                'log' => $request['subject'],
                'date' => time(),
                'type' => 'message',
                'formatted_date' => date_format(date_create(date('Y-m-d')),"F dS, Y"),
                'action_user_id' => $id,
                'action_name' => $user->name,
                'action_username' => $user->username,
                'link' => url('mailbox'),
                'profile_picture' => $user->profile_picture
            ]);
        }
    }

    if ($mailbox != "") {
        flash(trans('Your message has been sent'), 'success');
    } else {
        flash(trans('Your message has been not sent'), 'warning');
    }

    return \Redirect::route('mailbox')
        ->with( 'tabname','inbox' );

    }
    else{
        return redirect('home');
    }
}

How can I change this to input checkbox?

<section>
  <label class="input">
  <label class="select">
  <select name="role" id="role" style="width:100%">
    <option value="">Select Role</option>
    <option selected value="1" <?php if(isset($replyMessage)){if($role_id->role_id == 1){ echo "selected"; } } ?>>Individuals</option>
    <option value="100" <?php if(isset($replyMessage)){if($role_id->role_id == 100){ echo "selected"; } }?>>Organizations</option>
  </select>

<section>
  <label class="input">
    <label class="select">
      <select name="target_id[]" id="target_user" class="target_user" style="width:100%"  multiple="multiple">
        @if(!isset($replyMessage))
          @foreach($users->contact as $user)
            <option value="{{ $user->id }}" <?php if(isset($replyMessage)){ if($replyMessage == $user->id) { echo "selected"; } } ?>>{{ $user->username }} 
            </option>
          @endforeach
        @endif
      </select>
    </label>
  </label>
</section>

Here is what I tried:

<section>
  <label class="select">
    <select name="target_id[]" id="target_user" class="target_user" style="width:100%"  multiple="multiple">
      @if(!isset($replyMessage))
        @foreach($users->contact as $user)
          <input type="checkbox" value="{{ $user->id }}" <?php if(isset($replyMessage)){ if($replyMessage == $user->id) { echo "selected"; } } ?>>{{ $user->username }}>
        @endforeach
      @endif

  </label>
</section>
Andrew
  • 404
  • 4
  • 14
  • the ONLY valid child elements of a `SELECT` menu is `option` or `optgroup` – Professor Abronsius Oct 08 '19 at 06:06
  • Check this [thread](https://laracasts.com/discuss/channels/laravel/how-to-switch-select-option-tags-to-input-checkbox) – Zain Farooq Oct 08 '19 at 06:07
  • there is unbalanced curly braces in your controller. check this firstly, edit code. and then tell us exactly where are you getting error – Roman Meyer Oct 08 '19 at 06:19
  • This question is duplicated. Please see [this answer](https://stackoverflow.com/questions/25016848/bootstrap-putting-checkbox-in-a-dropdown) – Eyad Jaabo Oct 08 '19 at 06:20
  • @EyadJaabo isn't what I wanted. – Andrew Oct 08 '19 at 06:21
  • @Andrew you just need checkboxes inside select? – Eyad Jaabo Oct 08 '19 at 06:23
  • @RomanBobrik ok, not that error is important. My select-option is working right now, I just want to change this in some checkboxes. Here is an example: https://imgur.com/a/t83a27P . In the first photo, is what I actually have, and in the second photo is what I want to do with that. – Andrew Oct 08 '19 at 06:23
  • @EyadJaabo instead of select- option, not inside :) – Andrew Oct 08 '19 at 06:26

1 Answers1

1

Simple remove select element completely and add input checkbox

<section>
  <label class="select">
     @if(!isset($replyMessage))
       @foreach($users->contact as $user)
         <input name="target_id[]" type="checkbox" value="{{ $user->id }}" <?php if(isset($replyMessage)){ if($replyMessage == $user->id) { echo "selected"; } } ?>>{{ $user->username }}
       @endforeach
     @endif
   </label>
</section> 
Sachin
  • 789
  • 5
  • 18