[英]laravel loop value of table then save it to database
how to loop the value of ** table** then save it to database, i'm having difficulty on how to loop it, meaning save all value here is the link sample https://bootsnipp.com/snippets/featured/dynamic-table-row-creation-and-deletion .如何循环**表**的值然后将其保存到数据库中,我在如何循环它方面遇到困难,这意味着保存所有值这里是链接示例https://bootsnipp.com/snippets/featured/dynamic -table-row-creation-and-deletion 。
controller;控制器;
public function store(Request $request)
{
$this->validate($request, [
'student_name'=>'required|max:50',
]);
$leads=new Lead();
$leads->student_name = $request->student_name;
$leads->gender = $request->gender;
$leads->age = $request->age;
$leads->school = $request->age;
$leads->save();
}
}
//Display a successful message upon save
return redirect()->route('leads.index')
->with('flash_message', 'Success.');
}
VIEW.CREATE查看.创建
$(document).ready(function() { var i = 1; $("#add_row").click(function() { $('#addr' + i).html("<td>" + (i + 1) + "</td><td><input name='student_name" + i + "' type='text' placeholder='STUDENT NAME ' class='form-control input-md' /> </td><td><input name='gender" + i + "' type='text' placeholder='GENDER' class='form-control input-md'></td><td><input name='age" + i + "' type='text' placeholder='AGE' class='form-control input-md'></td><td><input name='school" + i + "' type='text' placeholder='SCHOOL' class='form-control input-md'></td>"); $('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>'); i++; }); $("#delete_row").click(function() { if (i > 1) { $("#addr" + (i - 1)).html(''); i--; } }); })
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> <div class="row"> <form action="{{route('leads.store')}}" method="POST"> {{csrf_field()}} <div class="col-sm-12"> <div class="row clearfix"> <div class="col-md-12 column"> <table class="table table-bordered table-hover" id="tab_logic"> <thead> <tr > <th class="text-center"> # </th> <th class="text-center"> STUDENT NAME </th> <th class="text-center"> GENDER </th> <th class="text-center"> AGE </th> <th class="text-center"> SCHOOL </th> </tr> </thead> <tbody> <tr id='addr0'> <td> 1 </td> <td> <input type="text" name='student_name' placeholder='STUDENT NAME' class="form-control"/> </td> <td> <input type="text" name='gender' placeholder='GENDER' class="form-control"/> </td> <td> <input type="text" name='age' placeholder='AGE' class="form-control"/> </td> <td> <input type="text" name='school' placeholder='SCHOOL' class="form-control"/> </td> </tr> <tr id='addr1'></tr> </tbody> </table> </div> </div> <a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a> <div> <button type="submit" class="btn btn-success">SUBMIT</button> <a href="{{route('leads.index')}}" class="btn btn-success">CANCEL</a> </div> </form> <!--end of container--> </div> </div>
You should take a look at sending arrays.你应该看看发送数组。
By appending []
to a input's name, you create a list of values.通过将[]
附加到输入的名称,您可以创建一个值列表。 (Instead of using numbers to give them a unique name. (而不是使用数字给它们一个唯一的名称。
<input name="names[]" value="Hello" />
<input name="names[]" value="World" />
<input name="names[]" value="Test" />
Sending the above form will give you the following result:发送上述表格将为您提供以下结果:
$names = $request->get('names'); // ["Hello", "World", "Test"]
Validating arrays also works a bit different.验证数组的工作方式也略有不同。 Take a look at the Laravel documentation for that: https://laravel.com/docs/5.6/validation#validating-arrays看看 Laravel 文档: https ://laravel.com/docs/5.6/validation#validating-arrays
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.