splendidkeen
4 days ago
168
10
Laravel

select2: How to convert array to Collection?

Posted 4 days ago by splendidkeen

I am trying to provide a select option, where we can choose multiple friends (User.php), which will be looped through within our Controller.

<select class="js-example-basic-multiple" name="users[]" multiple="multiple">
    <optgroup label="Users">
        @foreach(Auth::user()->friends as $user)
        <option value="{{$user}}">{{$user->first_name}}</option>
        @endforeach
    </optgroup> 
</select>

Within the Controller, I am looping through the $request, which returns an Array.

if ($users = $request->input('users'))
{
        foreach($users as $user)
        {
          $user;
        }
}

How can I get the actual User Model within the Controller, not just an array, so I could access e.g. $user->id;?

Currently it returns:

Trying to get property 'id' of non-object

when I trigger

if ($users = $request->input('users'))
{
       foreach($users as $user)
       {
         $user->id;
       }
}

Please sign in or create an account to participate in this conversation.