php - 拉拉维尔 | 如何使用表单对数据库中的存储项目进行分组
问题描述
我正在使用一个定义了默认任务的数据库表,并且我在我的表单中使用了一个“foreach()”来循环它们。每个任务都有一个复选框,如果未选中,数据将不会进入数据库。当该特定项目只定义了 1 个任务时,这不是问题。但是当定义了多个任务时,它只会选择最后一个任务并将其存储到数据库中。我需要将它们全部存储起来,而不仅仅是最后一项任务。
当复选框未选中时,我正在使用 jQuery 禁用字段
这是我的表单代码:
<form id="task-form"
class="uk-form-stacked"
method="post"
action="{{ route('project.storeTasks', ['project' => $project->id]) }}">
<table class="uk-table uk-table-middle">
<tr>
<th>Selecteer</th>
<th>Omschrijving</th>
<th>Toelichting</th>
<th>Uitvoering door</th>
<th>Budget</th>
<th>Deadline</th>
</tr>
@foreach($defaulttasks as $task)
<tr>
<td>
<input type="checkbox"
class="uk-checkbox selected"
name="tasks[{{ $task->id }}][checkbox]"
value="{{ $task->id }}"
checked>
</td>
<td>
<div class="uk-margin">
<div class="uk-form-controls">
<input type="text"
class="input uk-input uk-width-1-1"
name="tasks[{{ $task->id }}][title]"
value="{{ old('title', isset($task->title)
? $task->title
: null) }}">
</div>
</div>
</td>
<td>
<div class="uk-margin">
<div class="uk-form-controls">
<textarea class="input uk-textarea uk-width-1-1"
name="tasks[{{ $task->id }}][description]"
rows="1"
value="">{{ old('description', isset($task->description)
? $task->description
: null) }}</textarea>
</div>
</div>
</td>
<td>
<div class="uk-margin">
<div class="uk-form-controls">
<select class="input uk-select uk-width-1-1" name="tasks[{{ $task->id }}][teammember]">
{{-- @foreach($project->team as $teammember)
<!-- <option value="{{ $teammember->id }}">{{ $teammember->name }}</option> -->
@endforeach --}}
</select>
</div>
</div>
</td>
<td>
<div class="uk-margin">
<div class="uk-form-controls">
<input type="number"
class="input uk-input"
name="tasks[{{ $task->id }}][budget]"
step="any"
value="{{ old('budget', isset($task->budget)
? $task->budget
: null) }}">
</div>
</div>
</td>
<td>
<div class="uk-margin">
<div class="uk-form-controls">
<input type="text"
class="input uk-input"
name="tasks[{{ $task->id }}][deadline]"
value="{{ old('deadline', isset($task->deadline)
? $task->deadline
: null) }}">
</div>
</div>
</td>
<input class="input" type="hidden" name="project_id" value="{{ $project->id }}">
</tr>
@endforeach
{{ csrf_field() }}
</table>
</form>
这是我将项目存储到数据库中的功能:
public function quickAddTask(Projects $cached_projects, $project, Request $request)
{
$input = $request->all();
if(!empty($input['tasks']) && is_array($input['tasks']))
{
foreach($input['tasks'] as $id => $task)
{
// Create new task entry.
$new_task = new Task([
'project_id' => (integer) $input['project_id'],
'title' => $task['title'],
'description' => $task['description'],
'status' => (integer) 1,
'user_id' => (integer) 17,
'budget' => $task['budget'],
'deadline' => Carbon::parse($task['deadline'])->toDateString()
]);
// Find the project and attach the new task to the project.
$project = $cached_projects->find($project);
$project->tasks()->save($new_task);
// Flush cache.
Cache::tags('tasks')->flush();
// Set message and reload the tasks view.
setMessage('De taken zijn met succes toegevoegd aan het project.', 'success');
return redirect()->route('project.tasks', ['project' => $project->id]);
}
}
else
{
// Find the project.
$project = $cached_projects->find($project);
// Set message and reload the notes view.
setMessage('Er zijn geen taken aan het project toegevoegd.', 'danger');
return redirect()->route('project.tasks', ['project' => $project->id]);
}
}
如果你们需要更多信息,请告诉我!
谢谢。
编辑:通过我在 FrankerZ 的帮助下所做的更改,它只存储第一个选定的任务,而不是所有选定的任务。
解决方案
您需要将输入元素转换为数组。如果您<input name="test" />
在一个网页上多次输出,则在向服务器发送请求时只提交最后一次输入。task[$id]
当您使用括号表示法(即。 )时,PHP 自动支持转换为数组。转换您的字段:
<tr>
<td>
<input type="checkbox"
class="uk-checkbox selected"
name="tasks[{{ $task->id }}][checkbox]"
value="{{ $task->id }}"
checked>
</td>
<td>
<div class="uk-margin">
<div class="uk-form-controls">
<input type="text"
class="input uk-input uk-width-1-1"
name="tasks[{{ $task->id }}][title]"
value="{{ old('title', isset($task->title)
? $task->title
: null) }}">
</div>
</div>
</td>
<td>
<div class="uk-margin">
<div class="uk-form-controls">
<textarea class="input uk-textarea uk-width-1-1"
name="tasks[{{ $task->id }}][description]"
rows="1"
value="">{{ old('description', isset($task->description)
? $task->description
: null) }}</textarea>
</div>
</div>
</td>
<td>
<div class="uk-margin">
<div class="uk-form-controls">
<input type="number"
class="input uk-input"
name="tasks[{{ $task->id }}][budget]"
step="any"
value="{{ old('budget', isset($task->budget)
? $task->budget
: null) }}">
</div>
</div>
</td>
<td>
<div class="uk-margin">
<div class="uk-form-controls">
<input type="text"
class="input uk-input"
name="tasks[{{ $task->id }}]deadline"
value="{{ old('deadline', isset($task->deadline)
? $task->deadline
: null) }}">
</div>
</div>
</td>
<input class="input" type="hidden" name="project_id" value="{{ $project->id }}">
</tr>
然后你可以像这样在 laravel 中循环:
$input = $request->all();
if(!empty($input['tasks']) && is_array($input['tasks']))
{
foreach ($input['tasks'] as $id => $task) {
// Create new task entry.
$new_task = new Task([
'project_id' => (integer) $input['project_id'],
'title' => $task['title'],
'description' => $task['description'],
'status' => (integer) 1,
'user_id' => (integer) 17,
'budget' => $task['budget'],
'deadline' => Carbon::parse($task['deadline'])->toDateString()
]);
// Find the project and attach the new task to the project.
$project = $cached_projects->find($project);
$project->tasks()->save($new_task);
}
// Flush cache.
Cache::tags('tasks')->flush();
// Set message and reload the tasks view.
setMessage('De taak is met succes toegevoegd aan het project.', 'success');
return redirect()->route('project.tasks', ['project' => $project->id]);
}
else
{
// Find the project.
$project = $cached_projects->find($project);
setMessage('Er zijn geen taken aan het project toegevoegd.', 'danger');
return redirect()->route('project.tasks', ['project' => $project->id]);
}
推荐阅读
- javascript - .then 在 promise 解决之前触发 - $.getJSON
- python - Python中给定数字的阶乘的单行输出
- ruby - 使用 SSL、cookie、标头在站点上进行身份验证 - 不起作用
- python - 在 Pyspark 中选择一个范围
- css - 如何在 CSS 中更改默认的蓝色选择选项颜色
- android - 服务崩溃
- android - ACR39U ICC_Timeout 问题
- angularjs - md-tabs :当标签数量超过时,无法看到分页箭头
- python - Python+Selenium [Errno 2]:没有这样的文件或目录:'/tmp/tmpUgpMnz/webdriver-py-profilecopy'
- java - 数据流拆分表 ReadFn 不使用多个工作人员