How to give task to multiple users by filling same form in laravel?

  eloquent, laravel, laravel-5, laravel-7, laravel-8

i make a task manager for practice. I have 1 query that how can i assign task to multiple users by filling 1 form

This is my TaskController

public function store(Request $request)
{
    $request->validate([
        'staff_id' => 'required',
        'task' => 'required',
        'done_at' => 'sometimes',
    ]);

    $task = Task::create([
        'staff_id' => $request->input('staff_id'),
        'task' => $request->input('task'),
        'done_at' => $request->input('done_at'),
    ]);

    return redirect()->route('tasks.index')->withSuccess('Done');
}
public function complete($id)
{
    $task = Task::find($id);
    if ($task->done_at !== null) {
        $task->done_at = null;

        $task->save();
    } else {
        $task->done_at = now();

        $task->save();
}

This is my task model the task model having relation with another table name staffs

use HasFactory;

protected $fillable = [
    'staff_id',
    'task',
];

public function staff()
{
    return $this->belongsTo(Staff::class);
}

This is my create page where there is a form

<form method="post" action="{{route('tasks.store')}}">
        @csrf
        <table>
                <tr>
                    <td>Staff Name : </td>
                    <td>
                        <select name="staff_id" id="staff_id">
                            <option value="">Select One</option>
                            @foreach ($staffs as $staff)

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

                            @endforeach
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Task : </td>
                    <td><input type="text" name="task" class="form-control"></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" id="submit">Submit</button></td>
        </table>
    </form>

and this is my tasks table and this is my tasks table and this is my tasks table and this is my tasks table.

Schema::create('tasks', function (Blueprint $table) {
        $table->id();
        $table->bigInteger('staff_id')->nullable();
        $table->string('task')->nullable();
        $table->dateTime('done_at')->nullable();
        $table->timestamps();
    });

Source: Laravel

Leave a Reply