Laravel 5.6 i want to add more fields dynamically using jquery

Posted in CategoryLaravel
  • H
    Hardik Savani 2 weeks ago

    You need to follow this article: https://itsolutionstuff.com/post/laravel-dynamically-add-or-remove-input-fields-using-jqueryexample.html and you have to change controller method this way:

    public function addMorePost(Request $request)
    {
        $rules = [];
    
        foreach($request->input('name') as $key => $value) {
            $rules["name.{$key}"] = 'required';
        }
    
        foreach($request->input('fateherName') as $key => $value) {
            $rules["fateherName.{$key}"] = 'required';
        }
    
        $validator = Validator::make($request->all(), $rules);
    
        if ($validator->passes()) {
    
            foreach($request->input('name') as $key => $value) {
                TagList::create(['name'=>$value]);
            }
    
            foreach($request->input('fateherName') as $key => $value) {
                YourModel::create(['fateherName'=>$value]);
            }
    
            return response()->json(['success'=>'done']);
        }
    
        return response()->json(['error'=>$validator->errors()->all()]);
    }

    Try this way....

  • L
    Lasa Marndi 2 weeks ago

    Laravel - Dynamically Add or Remove input fields using JQuery

     

    if i add multiple felid then how dynamically insert data to database

     

    exmple: <input type="text" name=name[]>

    <input type="text" name=fateherName[]>

     

    any body please help me

Please login or register to leave a response.

* You May Also Like Bellow Issue to check *