2

I'm trying to create a helper to centralize a complicated Form::select.

My helper:

namespace App\Helpers;
use Log;
use App\Countries;

class Address {
    public static function country() {
        $countries = Countries::orderby('name_zh','asc');
        $form = "Form::select('countries', \$countries);";
        return $form;
    }
}

My View:

{!! Address::country() !!}

I would like to have this select form with $countries variable from this helper and show a Dropdown list on my view. How do I do it?

Kelvin Low
  • 390
  • 6
  • 22
  • Have you registered your helper class to `composer.json` file? And then you can use like: `{{ country($your_blade_file_variable) }}` Let me know if this works! – Hiren Gohel Nov 16 '17 at 10:01
  • Yes to I did register my helper class and it works. – Kelvin Low Nov 16 '17 at 10:05
  • My question is to use `$countries` from this helper in my view. Is this possible? – Kelvin Low Nov 16 '17 at 10:05
  • You want countries or the select html code ?? – Maraboc Nov 16 '17 at 10:07
  • I want my view to display a form select with array `$countries`. This `$countries` array is generate inside this helper. Is this possible? – Kelvin Low Nov 16 '17 at 10:12
  • You were `return $form;` in your helper, so that you can access it like: `{{ country($your_blade_file_variable) }}` in your blade file! – Hiren Gohel Nov 16 '17 at 10:12
  • Or variables can be only compacted/passing from controller? – Kelvin Low Nov 16 '17 at 10:12
  • You can get idea from this question's answer!: https://stackoverflow.com/questions/32430255/pass-a-custom-function-to-laravel-blade-template – Hiren Gohel Nov 16 '17 at 10:21
  • Thank Gobel, but I would like to pass variable from my helper. For the question's answer you sent me, it's having a variable `$string` from somewhere. I would like a helper having its own array and to show a form select on my view. Is this possible? – Kelvin Low Nov 16 '17 at 10:26
  • Try below answer, is that works for you which is edited? – Hiren Gohel Nov 16 '17 at 10:28

2 Answers2

1

In your helper create the select using the Laravel Collective helper the return it t the view, do it like this:

public static function country() {
    $countries = Countries::orderby('name_zh','asc')
                            ->pluck('name', 'id');
    return Form::select('countries', $countries);
}

In the view just call the method :

{{ Address::country() }}

Or if you want the $countries variable as well you can send it to the view from the controller then send it to the helper as well :

public function controllerFunction() {
    $countries = Countries::orderby('name_zh','asc')
                            ->get();
    return view('yourView')->withCountries($countries);
}

And in the view you have access to this $countries then pass it to the helper :

{{ Address::country($countries->pluck('name', 'id')) }}

And the helper this time should look like this :

public static function country($countries) {
    return Form::select('countries', $countries);
}
Maraboc
  • 10,550
  • 3
  • 37
  • 48
  • Thank Maraboc but no luck. This is what I get: `exception 'ErrorException' with message 'Invalid argument supplied for foreach()' in C:\wamp64\www\Fundmall\vendor\laravelcollective\html\src\FormBuilder.php:543 Stack trace: #0 ` What does this mean for? – Kelvin Low Nov 16 '17 at 10:19
  • Can you show me the output of `dd($countries);` in the helper before the return statment ? – Maraboc Nov 16 '17 at 10:29
  • I used dd($countries) and found it's my mistake. Your solution is now working, thanks Maraboc! – Kelvin Low Nov 16 '17 at 10:41
  • Happy to help ;) – Maraboc Nov 16 '17 at 10:58
0

Not sure to really understand your question...

But if you want to pass your $countries variable to your view, your function have to simply return this variable.

public static function country()
{
    $countries = ...;
    return $countries;
}

Then, two choices :

  • Use your function into your controller

    use App\Helpers\Address;
    // code code code
    public function controllerFunc()
    {
        // code
        $countries = Address::country();
    
        return view('yourview', compact($countries));
    }
    

    And get it in your view :

    <!-- Dump your $countries (from controller) var -->
    {{ dd($countries) }}
    
  • Use your function into your view

    <!-- Dump your $countries (from helper) var -->
    {{ dd(\App\Helpers\Address::country()) }}
    

Now, if you want to pass directly your select, then in your static function, return Form::select('countries', $countries') like Maraboc said.

Kévin Bibollet
  • 3,573
  • 1
  • 15
  • 33