copain

save value of an array

Posted 8 months ago by copain

how do i save the value of array into my controller?

result of array using DD

  dd($request->all());

array:4 [▼
  "_token" => "kjYvkKfuOkiBacn1EC0EzfeK5AitRYbXADE2E5RI"
  "student_name" => array:3 [▼
    0 => "ROBERT"
    1 => "cindy"
    2 => "rico"
  ]
  "age" => array:3 [▼
    0 => "12"
    1 => "13"
    2 => "14"
  ]
  "gender" => array:3 [▼
    0 => "MALE"
    1 => "female"
    2 => "MALE"
  ]
]

CONTROLLER:

public function store(Request $request)
    {
     dd($request->all());
    
        $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->save();
                    
                
                

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

HERE'S my VIEW

 <form action="{{route('leads.store')}}" method="POST">
    {{csrf_field()}}
    
    <div class="col-sm-12">
    
    
    <div class="container">
    <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="field-name">Field Name</label>
                        <input type="text" class="form-control" id="field-name"  name="student_name[]" placeholder="Field Name">
                    </div>
                    <span>-</span>
                    <div class="form-group">
                        <label class="sr-only" for="field-value">Field Value</label>
                        <input type="text" class="form-control" id="field-value"  name="age[]" placeholder="Field Value">
                    </div>
                    <div class="form-group">
                        <label class="sr-only" for="field-agent">Field Agent</label>
                        <input type="text" class="form-control" id="field-agent"  name="gender[]" placeholder="Field Agent">
                    </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 -->
</div>
          
<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>

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.