Reputation: 147
I am creating a cms type of application. For that while inserting users for their roles i.e.Administrator, Subscriber and Author, I am using pluck() method to retrieve data from roles table and display role in select dropdown as array but the problem is it is not working as it says [htmlspecialchars() expects parameter 1 to be string, array given] Please help me how can i fix this or are there are any other methods that can be used instead of pluck?.
AdminPostsController:
public function create()
{
$roles = Role::pluck('name', 'id')->all();
return view('admin.users.create', compact('roles'));
}
Create Page(create.blade.php):
<div class = "form-group">
{!! Form::label('role_id', 'Role:') !!}
{!! Form::text('role_id', array(''=>'Choose Your Role')+$roles, null,
['class'=>'form-control']) !!}
</div>
Please help!
Upvotes: 1
Views: 5547
Reputation: 746
AdminController should looks like:
public function create()
{
$roles = Role::pluck('name', 'id')->toArray();
return view('admin.users.create', compact('roles'));
}
And use blade Forms like below:
{!! Form::select('role_id', [null => 'Choose your role'] + $roles, null,
['class' => 'form-control']) !!}
You can use default value too by passing something to the 3rd position at Form::select(). Eg.
{!! Form::select('INPUT_NAME', ['your' => 'array', 'with' => 'options], DEFAULT_VALUE, ['class' => 'example class',]]) !!}
Laravel Collective documentation.
Upvotes: 1
Reputation: 10254
Your problem isn't on pluck
method, but on your blade view:
<div class = "form-group">
{!! Form::label('role_id', 'Role:') !!}
{!! Form::text('role_id', array(''=>'Choose Your Role')+$roles, null,
['class'=>'form-control']) !!}
</div>
Form::text
method expects a string as 2nd parameter, you gave an array.
Use Form::select
instead if you want a select.
Upvotes: 7