Swaylay Swaylay - 2 months ago 17
PHP Question

Laravel 5.2 Blade array is displaying as object

I have a dropdown / select box. I'm using this code to obtain values (commented // lines indicates different things I've tried:

public static function getCategories(){
return self::groupBy('category')->get(['category']);
//orderBy('category','asc')->groupBy('category')->get(['category']);
//distinct()->get(['category']);
}


On my View, in the dropdown / select box I get the results in an object/entity format:


Select Category
{"category":"Testing 1"}
{"category":"Testing 2"}
{"category":"Testing 3"}
{"category":"Testing 4"}

These are all unique values as expected; however, I only want to display the value and not object.


Reading Laravel 5.2+ documentation I see that I should use pluck().
Unfortunately,I haven't found a method that works (I'm a 1-2 year old PHP noob).

This is a blog application:
The "new blog post" form is where there is field for category. My client doesn't want a dropdown / select box to select their category;


TL:DR
I'm searching for unique values in the category column on the blog table to display in a dropdown / select box on my applications' sidebar to allow a search for posts with the category tag.


Thanks in advance;

Answer

If you are using the Form::select() to build the widget then it expects an associative key=>value array. So your getCategories() method could be the following:

public static function getCategories(){
    return self::groupBy('category')->lists('category', 'category');
}