Support the ongoing development of Laravel.io →
posted 7 years ago
Input

Hello!

For our Laravel website, in our Admin area we can create blog posts. For each blog post we populate a 'type' field. Currently this field is single choice however we'd like to make it multiple choice.

Here is the relevant code for this section:

@php
	// non-editable fields
	$protected = [
		'id',
		'created_at',
		'updated_at',
		'deleted_at'
	];
	
	// model prefix
	$prefix = class_basename($model);
	// input auto build
	$fields = [
		'id' => $model->id,
		'title' => $model->title,
		'description' => [
			'value' => $model->description,
			'input' => 'textarea',
		],
		'focus' => [
			'value' => !empty($model->focus_id) ? App\Models\Yoga::focus($model->focus_id) : null,
			'input' => 'select',
			'values' => App\Models\Yoga::focus(),
			'name' => 'focus_id'
		],
		'type' => [
			'value' => !empty($model->type_id) ? App\Models\Yoga::poseType($model->type_id) : null,
			'input' => 'select',
			'values' => App\Models\Yoga::poseType(),
			'name' => 'type_id'
		],		
		'course' => [
			'value' => $model->course ? $model->course->title : null,
			'input' => 'select',
			'values' => App\Helpers\InputList::fieldsFromModel(App\Models\Course::all(), 'id', 'title'),
			'name' => 'course_id'
		],
		'active' => [
			'value' => $model->active ? 'Yes' : 'No',
			'checked' => $model->active ? true : false,
			'input' => 'checkbox',
			'values' => [1]
		],
		'searchable' => [
			'value' => $model->searchable ? 'Yes' : 'No',
			'checked' => $model->searchable ? true : false,
			'input' => 'checkbox',
			'values' => [1]
		],
		'top_pick' => [
			'value' => $model->top_pick ? 'Yes' : 'No',
			'checked' => $model->top_pick ? true : false,
			'input' => 'checkbox',
			'values' => [1]
		],
		'teacher' => $model->teacher,
		'created_at' => !empty($model->created_at) ? $model->created_at->toDayDateTimeString() : null,
		'updated_at' => !empty($model->updated_at) ? $model->updated_at->toDayDateTimeString() : null,
		'length' => [
			'value' => $model->length,
			'input' => 'number',
		],
		'sortorder' => [
			'value' => $model->sortorder,
			'input' => 'number',
		] 
	];
@endphp

For the 'type' field it uses the checkbox blade file currently which is single choice: /resources/views/admin/includes/input-checkbox.blade.php which contains the following code:

<input id="{{ strtolower($prefix) . '_' . strtolower($key) }}" type="checkbox" name="{{ $prefix }}[{{ $key }}]" value="{{ array_shift($value['values']) }}" {{ $value['checked'] ? 'checked' : null }} class="form-control editable {{ $model->exists ? 'hidden' : null }}"/>

Could anyone help with what code I need to use in order to create a multiple choice option?

Thanks Simon

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Kalimukti kalimukti Joined 1 Oct 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.