Open with fade-zoom animation

Stay ahead...
Receive latest updates directly in your Inbox!

Adding custom attributes to Select field in laravelcollective HTML

Most of the time we use HTML select without any custom attributes. But in some cases adding custom attributes like data-* helps to easily access extra data related to the Option selected without making additional ajax call.

LaravelCollective HTML is an awesome package to created HTML forms in Laravel. It provides a lot of features to help to build simple and complex forms.

If you wanted to add some custom attributes in Select-Option tag, that is something like this:

This is easy if you do it using a Loop, but using loops for every select field where you want to add it can be a pain.

Luckily LaravelCollective-HTML has an option for it in "Form::select". So you don’t have to use Loops.
If you look into the Select method signature, it looks like this:

The last parameter is where you can pass extra attributes to be added in option tag.

So for the above example, the optionsAttributes parameter will be:

For example: If you want to show a list of tax rates:

I hope this small tip is helpful.
Although this option is present but is not described in the documentation so thought of sharing.

No Comments

Leave a comment

Your email address will not be published. Required fields are marked *