The select form is a bit complicated among other forms in Laravel collective. It is because we need to follow certain rules to make it work.

First of all the code of the form looks like this

Form::select("postId", $post, null, ['class' => 'form-control']);

Let me explain the code first.

We need to store the value so we have the name postID.

Then we have $post, because we need to dynamically populate the form and $post gives us all the posts.

The third parameter is the selected parameter which we don’t need if we are in adding data but not updating

And lastly, the fourth parameter lets us add class, placeholders, id, etcs

Now in the case of update

 {!! Form::select('manufacturer_country', [null => 'Select Country']+[\App\Models\Countries::pluck('country_name', 'country_id')->toArray()],
(isset($manufacturer) ? $manufacturer->country->country_id : null), ['class' => 'form-control']) !!}

we can use something like this.

Here I wanted to have products manufactured by the country where country and manufacture are in a one-to-many relationship. So first I provided the column where I want to add the data, then populated the select option with countries from the country model. The third parameter is checking if the form is added or edited with the help of the parameter $manufacturer providing the selected value if is the edit form. And lastly, I provided it with the class form-control.

Hope this helps you understand it because I didn’t at first.

Tweet
Share
Share
Pin