Как установить отключенную опцию выбора в Laravel?

В функции контроллера я извлекаю все attributes и атрибуты, которые я уже использовал.

Все атрибуты:

 $attributeNames = array('' => 'Select Attribute Name') + AttributeName::lists('name' , 'id'); 

Уже учтенные атрибуты:

 $selectedAttributeNames = $xmlDocument->masterInformation->masterAttributes; 

Как я могу сделать selectedAttributeNames disable пожалуйста?

Вот результат var_dump($selectedAttributeNames) :

 object(Illuminate\ Database\ Eloquent\ Collection) #312 (1) { ["items":protected]= > array(1) { [0] => object(MasterAttribute) #310 (20) { ["table":protected]= > string(16) "master_attribute" ["guarded": protected] => array(1) { [0] => string(2) "id" } ["connection": protected] => NULL["primaryKey": protected] => string(2) "id" ["perPage": protected] => int(15)["incrementing"] => bool(true)["timestamps"] => bool(true)["attributes": protected] => array(7) { ["id"] => int(1)["xpath"] => string(17) "this is the xpath" ["attribute_name_id"] => int(1)["master_information_id"] => int(6)["default_value"] => string(25) "This is the default value" ["created_at"] => string(19) "2014-07-19 17:53:55" ["updated_at"] => string(19) "2014-07-19 17:53:55" } ["original": protected] => array(7) { ["id"] => int(1)["xpath"] => string(17) "this is the xpath" ["attribute_name_id"] => int(1)["master_information_id"] => int(6)["default_value"] => string(25) "This is the default value" ["created_at"] => string(19) "2014-07-19 17:53:55" ["updated_at"] => string(19) "2014-07-19 17:53:55" } ["relations": protected] => array(0) {} ["hidden": protected] => array(0) {} ["visible": protected] => array(0) {} ["appends": protected] => array(0) {} ["fillable": protected] => array(0) {} ["dates": protected] => array(0) {} ["touches": protected] => array(0) {} ["observables": protected] => array(0) {} ["with": protected] => array(0) {} ["morphClass": protected] => NULL["exists"] => bool(true) } } } 

К сожалению, вспомогательный метод Form::select() Laravel не предоставляет способ задействовать процесс построения html для опций выбора.

При этом у вас есть несколько способов сделать это:

Во-первых: вы можете создать свой собственный макрос формы. Вот упрощенная версия

 Form::macro('select2', function($name, $list = [], $selected = null, $options = [], $disabled = []) { $html = '<select name="' . $name . '"'; foreach ($options as $attribute => $value) { $html .= ' ' . $attribute . '="' . $value . '"'; } $html .= '">'; foreach ($list as $value => $text) { $html .= '<option value="' . $value . '"' . ($value == $selected ? ' selected="selected"' : '') . (in_array($value, $disabled) ? ' disabled="disabled"' : '') . '>' . $text . '</option>'; } $html .= '</select>'; return $html; }); 

который вы можете зарегистрировать, например, в start.php .

Учитывая, что вы сначала получаете конвертацию Illuminate Collection с уже выбранными элементами в простой массив ключей

 $selectedAttributeNames = $xmlDocument->masterInformation->masterAttributes; $disabled = $selectedAttributeNames->toArray(); 

и создатели как $attributeNames и $disabled доступны в вашем представлении, вы можете использовать свой настраиваемый макрос, как это

 {{ Form::select2('mydropdown', $attributeNames, null, [], $disabled) }} 

Во-вторых: вы можете просто удалить (например, с помощью array_diff_key() ) уже выбранные элементы из своего массива параметров вместо их отключения:

 {{ Form::select('mydropdown2', array_diff_key($attributeNames, $disabled), null, []) }} 

В-третьих: по вашему мнению вы можете плюнуть на массив JavaScript уже выбранных атрибутов, которые необходимо отключить, а остальные – на стороне клиента с jQuery или vanilla JS.