0

In my database, I have a table for roles

there are three roles, Admin, Regional Admin, and User

From my controller, I'm trying to select roles where the role name does not equal to "Admin".

Following is my eloquent

$roles = Role::pluck('name','name')->where('name','<>','Admin')->get();

But this gives me an error saying

Too few arguments to function Illuminate\Support\Collection::get(), 0 passed..

When I changed my eloquent to

$roles = Role::pluck('name','name')->where('name','<>','Admin')->all();

it works but it'll give me all the records including the 'Admin'

Where do I need to fix in order to perform the following query,

SELECT * FROM `roles` WHERE name <> 'Admin'

I'm using those values in my blade as a dropdown options

{{ Form::select('roles',array_merge(['' => 'Veuillez sélectionner un rôle'],  $roles ), Request::old('Roles'),array('class' => 'form-control txt_txt')) }}
                
  • https://stackoverflow.com/questions/34587457/difference-between-eloquent-modelget-and-all – Pradeep Aug 14 '21 at 08:43
  • https://stackoverflow.com/questions/49421317/is-laravels-pluck-method-cheaper-than-a-general-get – Pradeep Aug 14 '21 at 08:52
  • I'm not even sure the `pluck()` method exists on the Eloquent query builder, only on the collection class, and you certainly shouldn't be using it before a WHERE clause. If you only need one field you should use `select()`. – Matthew Daly Aug 14 '21 at 09:33

2 Answers2

0

Hmm, I would try using != rather than <>, otherwise it seems fine :)

$roles = Role::pluck('name','name')->where('name','!=','Admin')->all();
Eric Qvarnström
  • 779
  • 6
  • 21
0

** Edited based on the change to question

// Get the name of all Roles except 'Admin' as array
$names = Role::where('name','<>','Admin')->pluck('name')->toArray();

// use $names as keys and values
$roles = array_combine($names, $names);

Then $roles can use in array_merge(['' => 'Veuillez sélectionner un rôle'], $roles ) to be the values using in blade.

label
  • 99
  • 6