copain
1 year ago

Loop and Array in La-ravel

Posted 1 year ago by copain

I have a student registration form, when you enroll your'e child you fill up the form then inside the form has information about the father like name, occupation,email and etc.. then inside the form also you can put your'e child's name, age gender etc .. now if you have multiple child you can click the + button to add another row for the second child then once you submit the form the two child you key-in will have the same father's information.

Question: how do i construct it into my Controller? and how do i loop the parent's information. Currently with my code i can only save array of child information i can't loop the father's information in each child.

Controller:

 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();
                     }
                 
}

VIEW

<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>

Please sign in or create an account to participate in this conversation.