There is a lists()
method that can clean up your code to a single line. :)
return Language::lists('translation_es', 'key')->toArray();
Thanks thomastkim! That's a nice edit. I removed the toArray() method chained on the end as apparently lists returns an array.
return Language::lists('translation_es', 'key');
No problem. :)
I can't test it right now, but it should be a collection. However, if you are making it an ajax call and returning it as a json response (for example), then Laravel will automagically convert it.
Interesting. I've also just read that in 5.x it now returns a collection rather than an array, but the code is working without toArray(). This is inside the lang folder, so Laravel itself is consuming the collection to get the correct translation. For example, in my Blade template i'm just doing {!! trans('myLangFile.myNamespace_keyvalue) !!}. I wonder what's going on behind the scenes...
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community