2

I make task manager there is a form where to assign task I want to do that if user select multiple option then it is seperated by comma

This is my form

<form method="post" action="{{route('assignments.store')}}">
        @csrf
        <table>
                <tr>
                    <td>Task Title : </td>
                    <td>
                        <select name="task_id" id="task_id">
                            @foreach ($tasks as $task)

                                <option value="{{ $task->id }}">{{ $task->title }}</option>

                            @endforeach
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Staff Name : </td>
                    <td>
                        <select name="staff_id" multiple>
                            <option value=null>Select One</option>
                            @foreach ($staffs as $staff)

                                <option value="{{ $staff->id }}">{{ $staff->name }}</option>

                            @endforeach
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Done At :</td>
                    <td><input type="time" name="done_at" class="form-control"></td>
                </tr>
            <td><button class="btn btn-primary" name="submit" type="submit" value="submit">Submit</button></td>
        </table>
    </form>

And this is my store function from where I storing the data recieved from form

$request->validate([
        'staff_id' => 'required',
        'task_id' => 'required',
        'done_at' => 'sometimes',
    ]);
    $assignment = new Assignment();
    $assignment->staff_id = $request->staff_id;
    $assignment->task_id = $request->task_id;
    $assignment->done_at = $request->done_at;
    $assignment->save();
    return redirect()->route('assignments.index', compact('assignment'))->withSuccess('Done');
Path Parakh
  • 177
  • 1
  • 1
  • 13
  • dose this answer your question? https://stackoverflow.com/questions/39686114/html-multiple-select-value-as-comma-separated-string-in-get-variable – Zia Yamin Apr 12 '21 at 04:50
  • Does this answer your question? [HTML Multiple Select value as comma separated string in GET variable](https://stackoverflow.com/questions/39686114/html-multiple-select-value-as-comma-separated-string-in-get-variable) – Zia Yamin Apr 12 '21 at 04:57
  • multiselect with name of type array, will give you multiple values, so in controller you will be able to get an array which will be actually comma seperated by default. Then store it as a json. – Akhtar Munir Apr 12 '21 at 08:15

1 Answers1

3

try this,

change the name=staff_id[]

<select id="staff" name="staff_id[]" multiple="multiple">
---
</select>

you can check the by

dd(implode(',', $request->staff_id));

$assignment = new Assignment();
$assignment->staff_id = implode(',', $request->staff_id);

And ya one more thing I forgot to tell you in your database I think staff_id is f.k so it will allow one id at the item to store so what you have to do CHANGE staff_id with type varchar then it will allow to store value with comma separate.

And below is the image form implode we are converting an array to string Please set select name = staff_id[] then only you will get multiple data

enter image description here

Yudiz Solutions
  • 4,216
  • 2
  • 7
  • 21