First of all, I'm very new to Laravel.
At the moment, I'm trying to make custom Macros for forms
in Laravel 5.6.27
This is what I have:
app/Services/Macros.php
<?php namespace App\Services;
use Collective\Html\FormBuilder;
class Macros extends FormBuilder {
public function selectState($name, $selected = null, $options = array())
{
$list = [
'' => 'Select One...',
'AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
'AR' => 'Arkansas',
'CA' => 'California',
'CO' => 'Colorado',
'CT' => 'Connecticut',
'DE' => 'Delaware',
'DC' => 'District of Columbia',
'FL' => 'Florida',
'GA' => 'Georgia',
'HI' => 'Hawaii',
'ID' => 'Idaho',
'IL' => 'Illinois',
'IN' => 'Indiana',
'IA' => 'Iowa',
'KS' => 'Kansas',
'KY' => 'Kentucky',
'LA' => 'Louisiana',
'ME' => 'Maine',
'MD' => 'Maryland',
'MA' => 'Massachusetts',
'MI' => 'Michigan',
'MN' => 'Minnesota',
'MS' => 'Mississippi',
'MO' => 'Missouri',
'MT' => 'Montana',
'NE' => 'Nebraska',
'NV' => 'Nevada',
'NH' => 'New Hampshire',
'NJ' => 'New Jersey',
'NM' => 'New Mexico',
'NY' => 'New York',
'NC' => 'North Carolina',
'ND' => 'North Dakota',
'OH' => 'Ohio',
'OK' => 'Oklahoma',
'OR' => 'Oregon',
'PA' => 'Pennsylvania',
'RI' => 'Rhode Island',
'SC' => 'South Carolina',
'SD' => 'South Dakota',
'TN' => 'Tennessee',
'TX' => 'Texas',
'UT' => 'Utah',
'VT' => 'Vermont',
'VA' => 'Virginia',
'WA' => 'Washington',
'WV' => 'West Virginia',
'WI' => 'Wisconsin',
'WY' => 'Wyoming'
];
return $this->select($name, $list, $selected, $options);
}
}
app/Providers/MacroServiceProvider.php
<?php namespace App\Providers;
use Collective\Html\HtmlServiceProvider;
class MacroServiceProvider extends HtmlServiceProvider {
public function register()
{
parent::register();
require base_path() . '/app/Services/Macros.php';
}
}
I also added in config/app.php
Collective\Html\HtmlServiceProvider::class,
App\Providers\MacroServiceProvider::class,
Then, in my view I put out this code:
{{ Form::selectState() }}
I get the following error:
"Method selectState does not exist. (View: C:\MAMP\htdocs\craftoesources\views\contact.blade.php)"
I have no idea what I'm doing wrong. Seems like I can't find the selectState
function that I have made. I had tried a lot of things, but I don't have a lot of knowledge about Laravel. Banging my head at the wall over it.
Can someone explain to me how Macros
work, and in specific the Form Macros
?
Thanks.
Don't override the library class. You will have a very hard time getting that to work.
FormBuilder
uses the Macroable
trait so to define macros you can do:
app/Providers/MacroServiceProvider.php
public function register()
{
parent::register();
FormBuilder::macro('selectState', function ($name, $selected = null, $options = array())
{
$list = [
'' => 'Select One...',
'AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
'AR' => 'Arkansas',
'CA' => 'California',
'CO' => 'Colorado',
'CT' => 'Connecticut',
'DE' => 'Delaware',
'DC' => 'District of Columbia',
'FL' => 'Florida',
'GA' => 'Georgia',
'HI' => 'Hawaii',
'ID' => 'Idaho',
'IL' => 'Illinois',
'IN' => 'Indiana',
'IA' => 'Iowa',
'KS' => 'Kansas',
'KY' => 'Kentucky',
'LA' => 'Louisiana',
'ME' => 'Maine',
'MD' => 'Maryland',
'MA' => 'Massachusetts',
'MI' => 'Michigan',
'MN' => 'Minnesota',
'MS' => 'Mississippi',
'MO' => 'Missouri',
'MT' => 'Montana',
'NE' => 'Nebraska',
'NV' => 'Nevada',
'NH' => 'New Hampshire',
'NJ' => 'New Jersey',
'NM' => 'New Mexico',
'NY' => 'New York',
'NC' => 'North Carolina',
'ND' => 'North Dakota',
'OH' => 'Ohio',
'OK' => 'Oklahoma',
'OR' => 'Oregon',
'PA' => 'Pennsylvania',
'RI' => 'Rhode Island',
'SC' => 'South Carolina',
'SD' => 'South Dakota',
'TN' => 'Tennessee',
'TX' => 'Texas',
'UT' => 'Utah',
'VT' => 'Vermont',
'VA' => 'Virginia',
'WA' => 'Washington',
'WV' => 'West Virginia',
'WI' => 'Wisconsin',
'WY' => 'Wyoming'
];
return $this->select($name, $list, $selected, $options);
});
What the Macroable
trait does is essentially just hold an array of extra functions (macros) and call them via the __call
or __callStatic
magic method.
Of course this only works for methods not already defined on the base class