Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 902
Hola Avatar asked Mar 11 '17 07:03

Hola


2 Answers

$users = User::where('id', '!=', Auth::id())->get();

this one except current login user..you can set admin id...in such field.. or you can use this also..

$users = User::all()->except(Auth::id());

both are work!!

like image 140
Chandhru Sekar Avatar answered Oct 05 '22 13:10

Chandhru Sekar


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');
like image 24
Alexey Mezenin Avatar answered Oct 05 '22 13:10

Alexey Mezenin