I want to add a dropdown field picking up an array from config file. I have tried this but getting an error.
In config/gallery.php:
return [
'column_num' => [
''=>'-- Choose one --',
'one' => 'Category: One column',
'one-second'=> 'Category: Two columns',
'one-third'=> 'Category: Three columns',
'one-fourth'=> 'Category: Four columns',
'one-fifth'=> 'Category: Five columns',
'one-sixth'=> 'Category: Six columns',
],
];
In app/PageTemplates.php
...
$this->crud->addField([
'name' => 'column_num',
'label' => 'Number of columns',
'type' => 'select_from_array',
'options' => config('gallery.column_num'),
'fake' => true,
'store_in' => 'extras',
]);
====== UPDATE =====
Error:
ErrorException in a1e88af9db4bb80b9055323bf64be621df2f9960.php line 19: Undefined index: value (View: /Users/sokada/Code/backpack-ceci/vendor/backpack/crud/src/resources/views/fields/select_from_array.blade.php) (View: /Users/sokada/Code/backpack-ceci/vendor/backpack/crud/src/resources/views/fields/select_from_array.blade.php) (View: /Users/sokada/Code/backpack- ceci/vendor/backpack/crud/src/resources/views/fields/select_from_array.blade.php) in a1e88af9db4bb80b9055323bf64be621df2f9960.php line 19 at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44 at
I tried this as in the doc, but it still gives an error.
$this->crud->addField([
// select_from_array
'name' => 'template',
'label' => "Template",
'type' => 'select_from_array',
'options' => [‘one’ => ‘One’, ‘two’ => ‘Two’],
'allows_null' => false,
// 'allows_multiple' => true, // OPTIONAL; needs you to cast this to array in your model;
]);
Error
ErrorException in PageTemplates.php line 114: Use of undefined constant ‘one’ - assumed '‘one’' in PageTemplates.php line 114 at HandleExceptions->handleError('8', 'Use of undefined constant ‘one’ - assumed '‘one’'', '/Users/sokada/Code/backpack-ceci/app/PageTemplates.php', '114', array()) in PageTemplates.php line 114
It is something to do with this if statement.
<option value="{{ $key }}"
@if ((isset($field['value']) && $key==$field['value'])
|| ( ! is_null( old($field['name']) ) && old($field['name']) == $key)
|| (is_array($field['value']) && in_array($key, $field['value'])) )
selected
@endif
>{{ $value }}</option>
For the moment I needed to modify
@foreach
statement invendor/backpack/crud/src/resources/views/fields/select_from_array.blade.php
to the following to avoid error.I hope tabacitu will figure it out a better solution.