How to use Macros in Laravel

Macros provide a way to add functionality to the classes in which you don’t have that function. It helps you to create a small reusable component that will be possible to use it anywhere in your application.

In this article, I will try to describe how to use Form macros in laravel.

First, you have to install laravelcollective package:

composer require laravelcollective/html

Then add the service provider and facades to your app.php config file:

'providers' => [
// ...
Collective\Html\HtmlServiceProvider::class,
// ...
],
'aliases' => [
// ...
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
// ...
],

Here is how to create form macros into your Laravel application.

First, you need to create a macros file, I have made an app/Services/Macros.php file, it doesn’t matter where you put it as long as you connect all of the components correctly.

We will start with this for now:

<?php
namespace App\Services;
use Collective\Html\FormBuilder;
class Macros extends FormBuilder {
   // code 
}

This is where we will write all of our macros, but now we have to describe in the application how to use it.

First, create a MacroServiceProvider.php file in app/Providers

and add it to your provider list in app.php. Note : If your laravel version is less than 5.3, You have to use bindShared method replacing singleton in MacrosServiceProvider.php.

'providers' => [
...
App\Providers\MacroServiceProvider::class,
];

Now, whatever form macros we set up in Macros.php will be available to our application. After adding one our final macros class will be as below:

Now you can call Form::selectBank() in any view, and have it display a nice list of Bank list to select from. You can specify the parameters as the input name, default selected value, and array of options.

Here is the example :

<div class="form-group">
<label for="bank_name">Bank Name</label>
{{ Form::selectBank("bank_name", $merchant['paymentInfo']->bank_name ?? null,["class"=>"form-control"]) }}
</div>

I have given an example using bank list. But if you need another list, suppose country list what will you do? It’s easy. In a similar way make a list and add it to macros class.

    /**
* Generate country drop down list
*
* @param $name
* @param null $selected
* @param array $options
* @return \Illuminate\Support\HtmlString
*/
public function selectCountry($name, $selected = null, $options = array())
{
$list = [
"" => "Select Country...",
"Afghanistan" => "Afghanistan",
"Albania" => "Albania",
"Algeria" => "Algeria",
"Argentina" => "Argentina",
"Armenia" => "Armenia",
"Australia" => "Australia",
"Austria" => "Austria",
"Azerbaijan" => "Azerbaijan",
"Bahrain" => "Bahrain",
"Bangladesh" => "Bangladesh",
"Belarus" => "Belarus",
"Belgium" => "Belgium",
// ..................
];
return $this->select($name, $list, $selected, $options);
}

After adding above function in Macros class, if you call from view Form::selectCountry(), you’ll get country list.

If you feel boring to configure manually, you can use this package. This package helps you to configure all the things using only two commands.

Thanks for reading.