Search code examples
phpjsonlaravel-5.5laravelcollective

How to populate Form::select() with JSON specified data in Laravel 5.5


I am having issues with populating Form::select() laravel collective component.

As this question describes : Foreach inside a Form::select in Laravel 4

I do not have a model for built in methods to get data, I am using guzzle to parse json data with custom methods, so either I do not have the "pluck()" method to pluck only names of something.so is there any other ways ?

Currently I have this, trying to make it work :

public function create()
{
    //
    $cat_array = null;
    $categories = $this->categories->all();

    if($categories['success']){

      foreach ($categories['message'] as $category) {
        $cat_array = array(
          $category['cat_name'],
        );
      }

    }

    return view('admin.default.pages.categories.create', compact('cat_array'));
}

And in create from :

{{ Form::select('cat_parent_id', $cat_array, null, ['placeholder' => 'None']) }}

Thanks in Advance !


Solution

  • You can still use collection methods but you need to manually make the collection with collect

    public function create()
    {
        //
        $cat_array = null;
        $categories = $this->categories->all();
    
        if($categories['success']){
           $cat_array = collect($categories['message'])->pluck("cat_name")->all();
        }
    
        return view('admin.default.pages.categories.create', compact('cat_array'));
    }