5 years ago

Best way to overwrite option method from Laravel 5 FormBuilder class?

Posted 5 years ago by mattkomarnicki


I would like to change the behavior of the method option from /illuminate/html/FormBuilder.php

The reason is that I want to change the way how "option" tags are generated using "Form::select" in my view.

Here is my class

<?php namespace Darwin\Services\Html;

class FormBuilder extends \Illuminate\Html\FormBuilder
    protected function option($display, $value, $selected)
        $selected = $this->getSelectedValue($value, $selected);

        $options = array('value' => e($value), 'selected' => $selected, 'this_is' => 'for_test'); //my trivial change

        return '<option'.$this->html->attributes($options).'>'.e($display).'</option>';

File is saved in app/Services/FormBuilder.php

Next I created Service Provider

<?php namespace Darwin\Providers;

use Illuminate\Support\ServiceProvider;

class MacroServiceProvider extends ServiceProvider {
    public function boot()

    public function register()
        // I don't know how to register my class

This example doesn't work in my case:

I tried using this->app->bind but with lack of success. When I dd('foo') inside register() it outputs "foo", but when I dd('bar') inside $this->app->bind('form', function($app) { it doesn't reach.

Of course 'Darwin\Providers\MacroServiceProvider' has been added into "providers" array in /config/app.php

I know that I'm doing something wrong but I need your help to figure out what exactly. Thanks!

Please sign in or create an account to participate in this conversation.