5

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)
        }
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Anastasie Laurent
  • 1,169
  • 6
  • 23
  • 35
  • Is [this question](http://stackoverflow.com/questions/24635869/using-laravel-form-class-to-add-the-disabled-attribute-for-some-options) similar to what you are after? – halfer Jul 19 '14 at 19:04
  • @halfer maybe I dont' know. That question is using blade code, but in my case I don't know how to use that because the list of selected attributes is in the controller. I can send it to the view but can you help me to build the `select` tag please? – Anastasie Laurent Jul 19 '14 at 19:12
  • 1
    Didn't understand clearly P – The Alpha Jul 19 '14 at 19:46
  • @WereWolf-TheAlpha Okay let me explain. I have a view to create something. that view has a form. the form has a select with options. when the use want to use that form again, the select element should have the already selected options as disabled. did you get me now please? – Anastasie Laurent Jul 19 '14 at 20:10
  • Can you please show what is in `$selectedAttributeNames` by var_dump($selectedAttributeNames)? – peterm Jul 19 '14 at 20:53
  • @peterm I updated the question with the information you asked for. – Anastasie Laurent Jul 19 '14 at 21:22
  • Your var_dump is unreadable. Please add some linebreaks... – ThiefMaster Jul 19 '14 at 21:22
  • @ThiefMaster is it readable now please? – Anastasie Laurent Jul 19 '14 at 21:24
  • @WereWolf-TheAlpha could you check my question here please ? http://stackoverflow.com/questions/25165241/laravel-php-add-to-associated-array-generates-new-nodes I appreciate your time and efforts – Anastasie Laurent Aug 06 '14 at 16:34

1 Answers1

3

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.

peterm
  • 91,357
  • 15
  • 148
  • 157
  • Thanks for the answer, now I am a little busy, tonight I will check it, +1 – Anastasie Laurent Jul 20 '14 at 05:43
  • 1
    This was helpful to me. There is a small typo on the line that sets the name attribute on the select element. It is missing the closing double quotation mark and should be $html = ' – Matty B Oct 22 '14 at 19:31