如何在laravel中使用带数组的循环

时间:2018-03-26 02:27:31

标签: php laravel

我有一个学生注册表,当你注册你的孩子时,你填写表格,然后在表格中有关于父亲的信息,如姓名,职业,电子邮件等。然后在表格里面你也可以把你孩子的姓名,年龄性别等等。现在,如果你有多个孩子,你可以点击+按钮为第二个孩子添加另一行,然后一旦你提交了表格,你输入了两个孩子 - in将拥有相同的父亲的信息。

问题:如何将其构建到我的控制器中?以及如何循环父母的信息。目前我的代码只能保存一系列子信息,我不能在每个孩子中循环父亲的信息。

控制器:

public function store(Request $request)
{
//dd($request->all());

            $this->validate($request, [
                 'student_name.*'=>'required|max:50',
                  'age.*'=>'required|integer',
               //Roles Goes Here 
             ]);

             $input = $request->all();

             if (count($input['student_name']) > 0) {
                 for ($i = 0 ; $i < count($input['student_name'])  ; $i++) {
                        $leads = new Lead();

                       $leads->student_name = $input['student_name'][$i];
                       $leads->gender =  $input['gender'][$i];
                       $leads->age = $input['age'][$i];

                       $leads->father_name=$request->father_name;
                       $leads->father_contact1=$request->father_contact1;
                       $leads->father_email=$request->father_email;
                       $leads->father_occupation=$request>father_occupation;
                       $leads->father_religion=$request->father_religion;

                       $leads->save();
                 }

查看:

&#13;
&#13;
<form action="{{route('leads.store')}}" method="POST">
    {{csrf_field()}}
	
    <div class="col-sm-12">
	<h3 class="well">Parent's Information</h3>
	<div class="row">
        

		<div class="col-sm-4 form-group">
		<label>FATHER'S NAME</label>					
		<input class="form-control" id="txtuppercase1"  type="text" name="father_name" value="{{old('father_name')}}" placeholder="Enter FATHER NAME.." >						
		</div>

		<div class="col-sm-4 form-group">
		<label>CONTACT</label>
		<input type="text" id="txtuppercase2"   name="father_contact1" value="{{old('father_contact1')}}" placeholder="Enter CONTACT.." class="form-control"  >
		</div>
		<div class="col-sm-4 form-group">

		<label>EMAIL ADDRESS</label>
		<input type="text"    name="father_email" value="{{old('father_email')}}" placeholder="Enter EMAIL ADDRESS.." class="form-control"  >
		</div>

		<div class="col-sm-4 form-group">
		<label>OCCUPATION</label>
		<input type="text"   name="father_occupation" value="{{old('father_occupation')}}" placeholder="Enter OCCUPATION.." class="form-control"  >
		</div>

		<div class="col-sm-4 form-group">
		<label>RELIGION</label>
		<input type="text"   name="father_religion" value="{{old('father_religion')}}" placeholder="Enter RELIGION.." class="form-control"  >
		</div>

    </div>
	<br>
    <div class="row">
        <div class="col-md-12">
            <div data-role="dynamic-fields">
                <div class="form-inline">
                    <div class="form-group">
                        <label class="sr-only" for="student_name">NAME</label>
                        <input type="text" class="form-control" id="student_name"  name="student_name[]" placeholder="STUDENT NAME">
                    </div>
                    <span>-</span>
                    <div class="form-group">
                        <label class="sr-only" for="age">AGE</label>
                        <input type="text" class="form-control" id="age"  name="age[]" placeholder="AGE">
                    </div>
					<span>-</span>
					
					<div class="form-group">
					    
                        <label class="sr-only" for="gender">GENDER</label>
                        <input type="text" class="form-control" id="gender"  name="gender[]" placeholder="GENDER">
                    </div>
					<span>-</span>
					
					<div class="form-group">
					    
                        <label class="sr-only" for="school">SCHOOL</label>
                        <input type="text" class="form-control" id="last_school_attended"  name="last_school_attended[]" placeholder="SCHOOL">
                    </div>
                    <button class="btn btn-danger" data-role="remove">
                        <span class="glyphicon glyphicon-remove"></span>
                    </button>
                    <button class="btn btn-primary" data-role="add">
                        <span class="glyphicon glyphicon-plus"></span>
                    </button>
                </div>  <!-- /div.form-inline -->
            </div>  <!-- /div[data-role="dynamic-fields"] -->
        </div>  <!-- /div.col-md-12 -->
    </div>  <!-- /div.row -->

	  <br>    
<div>
<button type="submit" class="btn btn-success">SUBMIT</button>
<a href="{{route('leads.index')}}" class="btn btn-success">CANCEL</a>
</div>
 </form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您不需要循环父母的信息。您可以通过$ input直接访问父母的信息[&#​​39; father_name&#39;];