[英]Inserting a two diminsionale array in database from multiple dynamic fields - Laravel
Using Laravel I created a form where dynamically input fields can be added and remove using jQuery. 使用Laravel,我创建了一个表单,可以在其中使用jQuery添加和删除动态输入字段。
I am now trying to insert the data into the database, there I encounter a problem. 我现在正在尝试将数据插入数据库,在那里我遇到了问题。 My database exists of two tables;
我的数据库有两个表; Tasks and issues.
任务和问题。 The 'issue' table is linked to the tasks table bij task_id.
“问题”表链接到任务表bij task_id。 This task_id should be also sent to the database but I can't get it, wright.
这个task_id也应该发送到数据库,但是我无法获取,赖特。 See my code below.
请参阅下面的代码。
Thanks for the help. 谢谢您的帮助。
public function store(Request $request, Issue $issue, Task $task)
{
## First method
// foreach($request->issueInfo as $key => $value) {
// Issue::create($value);
// }
## Second method
foreach ($request->issueInfo as $key => $name) {
dd($name);
$names[] = [
'task_id' => $task->id,
'issue_name' => $name,
'issue_time' => $name,
'issue_date' => $name,
'issue_type' => $name,
];
}
Issue::insert($names);
return back();
}
Route::post('/tasks/{task}/issues', 'IssueController@store');
@extends('layout')
@section('content')
<script>
$(document).ready(function() {
var i = 1;
$('.addmore').click(function(){
i++;
$('#dynamicFields').append('<div class="form-group"><label for="issue_name">Issue Name</label><select class="form-control" name="issueInfo['+i+'][issue_name]"><option></option><option value="Error">Error</option><option value="Grammer">Grammer</option><option value="Undefined">Undefined</option><option value="Typpo">Typpo</option><option value="No errors">No errors</option></select></div><div class="form-group"><label for="issue_date" class="label">Issue Date</label><input class="form-control" type="date" name="issueInfo['+i+'][issue_date]"></div><div class="form-group"><label for="issue_time" class="label">Issue Time</label><input class="form-control" type="time" name="issueInfo['+i+'][issue_time]" ></div><div class="form-group"><label for="issue_type" class="label">Issue Type</label><select class="form-control" name="issueInfo['+i+'][issue_type]"><option></option><option value="False45">False45</option><option value="False104">False104</option></select></div><div class="form-group"><button type="button" class="btn btn-danger remove-field">Remove</button></div>');
});
// Removing fields
$('#dynamicFields').on('click', '.remove-field', function(){
$(this).parents('div').remove(); i--;
})
});
</script>
<div class="form-group"><label for="issue_name">Name</label></div>
<head>Testing the creation of multiple fields</head>
<form action="/tasks/{{$task->id}}/issues" method="post" >
@csrf
<div id="dynamicFields">
<div class="form-group">
<label for="issue_name">Issue Name</label>
<select class="form-control" name="issueInfo[0][issue_name]">
<option></option>
<option value="Error">Error</option>
<option value="Grammer">Grammer</option>
<option value="Undefined">Undefined</option>
<option value="Typpo">Typpo</option>
<option value="No errors">No errors</option>
</select>
</div>
<div class="form-group">
<label for="issue_date" class="label">Issue Date</label>
<input class="form-control" type="date" name="issueInfo[0][issue_date]" value="{{old('issue_date')}}">
</div>
<div class="form-group">
<label for="issue_time" class="label">Issue Time</label>
<input class="form-control" type="time" name="issueInfo[0][issue_time]" value="{{old('issue_time')}}">
</div>
<div class="form-group">
<label for="issue_type" class="label">Issue Type</label>
<select class="form-control" name="issueInfo[0][issue_type]">
<option></option>
<option value="False45">False45</option>
<option value="False104">False104</option>
</select>
</div>
<div class="form-group">
<input type="button" name="submit" id="submit" class="btn btn-primary addmore" value="+" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">New Location</button>
</div>
</div>
</form>
@endsection
Output when I do dd($names) in my controller
array:1 [▼ 0 => array:5 [▼ "task_id" => 1 "issue_name" => array:4 [▼ "issue_name" => "Error" "issue_date" => null "issue_time" => null "issue_type" => null ] "issue_time" => array:4 [▼ "issue_name" => "Error" "issue_date" => null "issue_time" => null "issue_type" => null ] "issue_date" => array:4 [▶] "issue_type" => array:4 [▶] ] ]
Use built-in Eloquent methods to your advantage. 使用内置的雄辩方法可以发挥自己的优势。 First, define relationships:
首先,定义关系:
class Task
{
public function issues()
{
return $this->hasMany(Issues::class);
}
{
class Issue
{
public function task()
{
return $this->belongsTo(Task::class);
}
{
Next, write a good helper method. 接下来,编写一个好帮手方法。 Note the
create()
method already takes an array input, so you don't have to foreach
through all the array keys: 请注意,
create()
方法已经接受了数组输入,因此您不必foreach
所有数组键:
class Task
{
...
public function addIssue($issue)
{
return $this->issues()->create($issue);
}
}
Controller logic can be simplified, and this is a good opportunity to do some server-side validation: 控制器逻辑可以简化,这是进行一些服务器端验证的好机会:
public function store(Request $request, Task $task)
{
$attributes = request()->validate([
// issue validation rules here
]);
$task->addIssue($attributes);
return back();
}
You should try the following. 您应该尝试以下方法。 I adapted your code a bit in the fiddle.
我在小提琴中修改了您的代码。 Instead of pushing everything in the same array, store every element in a separate array.
而不是将所有内容都推送到同一数组中,而是将每个元素存储在单独的数组中。 In the fiddle, you find the adapt code.
在小提琴中,您可以找到适配代码。 In your controller, you should do the following.
在控制器中,您应该执行以下操作。 You should also remove Issue $issue because you are not using it.
您还应该删除Issue $ issue,因为您没有使用它。
public function store(Request $request, Task $task)
{
//
foreach ($request->issue_name as $index => $name) {
$task->issues()->create([
'issue_name' => $name,
'issue_time' => $request->issue_time[$index],
'issue_date' => $request->issue_date[$index],
'issue_type' => $request->issue_type[$index]
]);
}
return back();
}
To solve your problem with the remove button. 用删除按钮解决您的问题。 Add to your class row for example "remove-this-field".
添加到您的班级行,例如“删除此字段”。 And adapt your javascript code for the removal of the fields to:
并修改您的JavaScript代码以删除以下字段:
$('#dynamicFields').on('click', '.remove-fields', function(){
$('.remov-this-field').remove(); i--;
})
You could try adding hidden field to your form where you should set value of that field to task_id which then will be passed to Controller when you submit form. 您可以尝试在表单中添加隐藏字段,您应该将该字段的值设置为task_id,然后在提交表单时将其传递给Controller。
I think after that your First method should work properly. 我认为之后,您的First方法应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.