Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Giolf's avatar

Create a Form Macros in Laravel 5

Hi all, This is my first PHP Framework so i'm a noob about it, i'm sorry for that.

I decided to create my first Form Macros with this steps:

  1. I create a ServiceProvider
<?php
namespace App\Providers;

use Collective\Html\HtmlServiceProvider;

class MacroServiceProvider extends HtmlServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        // Macros must be loaded after the HTMLServiceProvider's
        // register method is called. Otherwise, csrf tokens
        // will not be generated
        parent::register();

        // Load macros
        require base_path() . '/resources/macros/people.php';
    }
}
  1. i create a new file people.php with inside the new FormMacro
<?php
/**
 * This file has the People's Form Macros.
 * It means: Teachers, Students and Contacts.
 */

Form::macro('active', function () {
    $html =
        '<select id="active" class="form-control" required>
            <option value="" disabled selected>Attivo</option>
            <option value="Si">Si</option>
            <option value="No">No</option>
        </select>';

    return $html;
});

Now if i try to use my new 'active' Form Macro:

{{ Form::active() }}

i get this error:

Method active does not exist. (View: C:\xampp\htdocs\supsi-progetto\resources\views\backend\new-student.blade.php)

Honestly i don't know where is the mistake. I just follow a discussion inside of this forum and i'm pretty sure that i did all the steps correctly. Probably is missing something but i don't know what.

Thank you

0 likes
3 replies
Giolf's avatar

someone can help me? thank you

shez1983's avatar

ok i am not sure what is going on.. but did you put you ServiceProvider in your config/app.php file in providers array?

can you make an error in your ServiceProvider you posted to see if laravel will detect it? this will let you know if laravel is loading/looking at this in the first place?

Please or to participate in this conversation.