In a controller function, I extract all the attributes
and the attributes that I have already used.
All the attributes:
$attributeNames = array('' => 'Select Attribute Name') + AttributeName::lists('name' , 'id');
Already Taken Attributes:
$selectedAttributeNames = $xmlDocument->masterInformation->masterAttributes;
How can I make the selectedAttributeNames
as disable
please?
Here is the output of 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)
}
}
}
Unfortunately Laravel's Form::select()
helper method doesn't provide a way to tap into the process of building html for select's options.
That being said you have several ways to go about it:
First: You can create your own form macro. Here is oversimplified version
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;
});
which you can register for example in start.php
.
Given that you first get convert Illuminate Collection with already selected items into a plain array of keys
$selectedAttributeNames = $xmlDocument->masterInformation->masterAttributes;
$disabled = $selectedAttributeNames->toArray();
and maker both $attributeNames
and $disabled
available in your view you can use your custom macro like this
{{ Form::select2('mydropdown', $attributeNames, null, [], $disabled) }}
Second: you can just remove (e.g. with array_diff_key()
) already selected items from your array of options instead of disabling them:
{{ Form::select('mydropdown2', array_diff_key($attributeNames, $disabled), null, []) }}
Third: in your view you can spit a JavaScript array of already selected attributes that need to be disabled and do the rest client-side with jQuery or vanilla JS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With