Search code examples
phplaravelformsselectlaravelcollective

How to edit Select tag in Laravel Collective?


I want to edit the task_status select but I get an invalid foreach() argument error. The format follows similarly to other forms from the Laravel Collective.

{{Form::select('task_status', $task->task_status, [ 'On Hold'=> 'On Hold', 'Completed'=> 'Completed'], ['class' => 'form-control'])}}

Solution

  • invalid foreach() argument error.

    This will occur only when you dont pass the values for populating the dropdown

    So You need to know the arguments

    function select(
            $name,
            $list = [],
            $selected = null,
            array $selectAttributes = [],
            array $optionsAttributes = [],
            array $optgroupsAttributes = []
        )
    

    First Argument => 'name of the select tag' in Your case its task_status

    Second Argument => 'Dropdown Values List ' in Your case its [ 'On Hold'=> 'On Hold', 'Completed'=> 'Completed']

    Third Argument => 'the selected option(s) ' in Your case its $task->task_status (Used while editing)

    Fourth Argument => 'optional Attributes ' in Your case its ['class' => 'form-control']

    So the final function may look like

    {!!Form::select('task_status',[ 'On Hold'=> 'On Hold', 'Completed'=> 'Completed'],$task->task_status,['class' => 'form-control'])!!}
    

    If You are using

    Form::model() to populate the values while editing you don't need to add $task->task_status

    to the select tag it will be automatically loaded

    ONLY WHILE EDITING