vendredi 20 juillet 2018

Laravel 5.6.27 - How can I make custom Form Macros?

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':

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:



I get the following error:

"Method selectState does not exist. (View: C:\MAMP\htdocs\crafto\resources\views\contact.blade.php)"

I have no idea what I'm doing wrong. Seems like he can't find the selectState function that I have made. 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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire