Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form select box {!!Form::select()!!} Laravel

I'm using Laravel Collective Form builder in my view and i'm populating this select box from a table in my DB like this

I am having an issue with my values not matching up and my dropdown is also giving me the values as an array...

Here's what I have in my PostsController:-

public function edit(Post $post)
{
    $categories = Category::all()->pluck('title', 'id')->toArray();

    return view('posts.edit')->withPost($post)->withCategories($categories);
}

and here's my view edit.blade.php:-

{{ Form::label('category_id', 'Category :')}}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}

So I need little help?

Here's the value issue I was talking about: enter image description here

Here's the array issue I was talking about: enter image description here

like image 905
Imagine R Power Avatar asked Nov 23 '25 08:11

Imagine R Power


1 Answers

no need to use toArray() pluck method automatically create an array. try this

$categories = Category::pluck('title', 'id');
like image 136
Matius Nugroho Aryanto Avatar answered Nov 25 '25 21:11

Matius Nugroho Aryanto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!