Tuesday, February 28, 2017

Laravel - Formatting string values in Collection retrieved via pluck()

I have a table abilities and it has some entries in it (sorry for not making it into a table format, I don't know how to do that):

-id|name
-1|create_user
-2|delete_user
-3|ban_user

Righty o, when I run the following code:

$abilities = App\Ability::all()->pluck('name','id')

I get the following output in tinker:

 Illuminate\Support\Collection {#861
 all: [
   1 => "create_user",
   2 => "delete_user",
   3 => "ban_user",
 ],

}

I want it to appear something like: 1=>"Create User"...i.e. underscore replaced with space and both words capitalised. How do I go about that? I am trying this out for the view.




via omrakhur

Advertisement