如何同时将任务分配给多个用户?
How to assign task to multiple users at same time?
我在做任务管理系统,不知道如何同时给多个用户分配任务?
这是我的 AssignmentController Store 函数。
public function store(Request $request)
{
$request->validate(array(
'assignment' => 'array|required',
'assignment.*.assigned_to' => 'required',
'task_title' => 'required',
'task_description' => 'required',
'done_at' => 'sometimes',
));
foreach($request->get('assignment') as $assignment)
$assignment = Assignment::create(array(
'task_title' => $request->input('task_title'),
'task_description' => $request->input('task_description'),
'assigned_to' => $assignment['assigned_to'],
'done_at' => $request->input('done_at'),
));
}
这是我的 assignment.create 页面,其中有 3 个字段 assigned_to、task_title 和 task_description done_at 是可选的
<form method="post" action="{{route('assignments.store')}}">
@csrf
<table>
<tr>
<td>Staff Name : </td>
<td>
<select name="assignment[{{$key}}][assigned_to]" id="assigned_to" multiple>
<option value="">Select One</option>
@foreach ($staffs as $staff)
<option value="{{ $staff->id }}">{{ $staff->name }}</option>
@endforeach
</select>
</td>
</tr>
<tr>
<td>Task Title : </td>
<td><input type="text" name="task_title" class="form-control"></td>
</tr>
<tr>
<td>Task Description : </td>
<td><input type="text" name="task_description" 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>
您有用户table
id
name
和作业table
id
title
description
而且你必须 user_assignment table
user_id
assignment_id
这个table定义了用户和分配之间的多对多关系
这意味着每个用户有很多任务,每个任务属于很多用户
阅读文档:
https://laravel.com/docs/7.x/eloquent-relationships#many-to-many
我在做任务管理系统,不知道如何同时给多个用户分配任务?
这是我的 AssignmentController Store 函数。
public function store(Request $request)
{
$request->validate(array(
'assignment' => 'array|required',
'assignment.*.assigned_to' => 'required',
'task_title' => 'required',
'task_description' => 'required',
'done_at' => 'sometimes',
));
foreach($request->get('assignment') as $assignment)
$assignment = Assignment::create(array(
'task_title' => $request->input('task_title'),
'task_description' => $request->input('task_description'),
'assigned_to' => $assignment['assigned_to'],
'done_at' => $request->input('done_at'),
));
}
这是我的 assignment.create 页面,其中有 3 个字段 assigned_to、task_title 和 task_description done_at 是可选的
<form method="post" action="{{route('assignments.store')}}">
@csrf
<table>
<tr>
<td>Staff Name : </td>
<td>
<select name="assignment[{{$key}}][assigned_to]" id="assigned_to" multiple>
<option value="">Select One</option>
@foreach ($staffs as $staff)
<option value="{{ $staff->id }}">{{ $staff->name }}</option>
@endforeach
</select>
</td>
</tr>
<tr>
<td>Task Title : </td>
<td><input type="text" name="task_title" class="form-control"></td>
</tr>
<tr>
<td>Task Description : </td>
<td><input type="text" name="task_description" 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>
您有用户table
id
name
和作业table
id
title
description
而且你必须 user_assignment table
user_id
assignment_id
这个table定义了用户和分配之间的多对多关系 这意味着每个用户有很多任务,每个任务属于很多用户
阅读文档: https://laravel.com/docs/7.x/eloquent-relationships#many-to-many