I have this array ( I displayed it with function dd() ), with multiple data, and I want to display only "name" data in input field.
That’s how is looking now
And how I want to be displayed names.
That’s my function from Controller that I push data to page.
private function needs()
{
$grades = Grade::all()->pluck('name', 'id')->toArray();
$subjects = Subject::all()->pluck('name', 'id')->toArray();
$students = User::students()->get()->pluck('name', 'id')->toArray();
$teachers = User::teachers()->get()->pluck('name', 'id')->toArray();
$goals = Goal::all()->pluck('name', 'id')->toArray();
$statuses = Status::all()->pluck('name', 'id')->toArray();
$formats = Format::all()->map->only(['id', 'name', 'students'])->values()->toArray();
return compact('grades', 'subjects', 'students', 'teachers', 'goals', 'statuses', 'formats');
}
And there is form from the page :
<div class="form-group">
{{ Form::label('format_id', 'Формат', ['class' => 'col-sm-3 control-label no-padding-right']) }}
<div class="col-sm-8">
{{ Form::select('format_id', [null => '--не выбран--'] + $formats, $data->format_id ?? 0, ['class' => 'form-control', 'id' => 'format_id']) }}
</div>
</div>
3
Answers
You can handle this with a little loop under the
$format
variable in your controller:Or change this line like this:
You need to transform your $formats array to have key "id" and value "name":
You can use mapWithKeys() collection function to do so.
Exemple with formating an array to [id => name]:
Documentation: https://laravel.com/docs/9.x/collections#method-mapwithkeys