Skip to content
Advertisement

How to get All data from a model except one?

I want to retrieve all users data except one.for that i used the following query

$users=User::whereNotIn('name',['admin'])->pluck('id','name');

When i dd() the output I see all the users data except the one But when I send the query results in foreach() loop in view page I see

Trying to get property of non-object

Error in view page.What’s the Error here? Can anyone suggest me please?

Here is the foreach loop i used in view page

@foreach($users as $user)
          <option value="{{$user->id}}">{{$user->name}}</option>                        
@endforeach

Advertisement

Answer

pluck() creates an array with [id => name] structure, so change the code in assign_rol‌​e.blade.php to:

@foreach ($users as $id => $name)
    <option value="{{ $id }}">{{ $name }}</option>                        
@endforeach

And pluck() parameters to:

->pluck('name', 'id');
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement