copain
254
9
Laravel

how to execute loop with array

Posted 1 year ago by copain

i have a form where you enter the parent information together with the child information now how do i save if one parent with multiple child.

Example:

FATHER (A) has child1,child2,child3 how do i loop this,

CONTROLLER:

public function store(Request $request)
    {
     
    
                $this->validate($request, [
                     'student_name.*'=>'required|max:50',
                      'age.*'=>'required|integer',
                  
                 ]);

                           /*$leads = new Lead();
                           
                           $leads->save();*/
               


                 $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->last_school_attended = $input['last_school_attended'][$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();
                     }
                 }
                
                

                //Display a successful message upon save
                return redirect()->route('leads.index')
            ->with('flash_message', 'Success.');
    }

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.