Tuesday, January 25, 2022

[FIXED] How to translate collections in Laravel 5?

Issue

I am using the pluck method to retrieve values. How can I translate these values? (these values are options for a selection input field)

$relationtypes = Relationtype::pluck('name', 'id');

My relationtypes are: supplier, customer, etc.


Solution

I also found a more convenient solution:

$relationtypes = RelationType::pluck('name', 'id')->map(function ($item, $key) {
    return trans('labels.' . $item . '');
});

Passing this to your view, you can use:

{!! Form::select('relationtypes[]', $relationtypes, 
    isset($relation) ? $relation->relationtypes->pluck('id')->toArray() : 0, ['class' => 'form-control']) !!}

Hope this helps other people!



Answered By - user3253002

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.