How to update data using ajax in laravel 5.8 [ Updated 2019 ]

How to update data using ajax in laravel 5.8 [ Updated 2019 ]

How to update data using ajax in laravel 5.8 [ Updated 2019 ]



Let’s see how to update data in database using ajax in laravel 5.8. Here is the proof that code is working





How to update data using ajax in laravel 5.8 [ Updated 2019 ]



So i have a database ready with category table in it.and i am fetching all the categories on view file as given below
How to update data using ajax in laravel 5.8 [ Updated 2019 ]
[ads]
route for above list of category is





       //getting all categories and displaying them
      Route::get('/post/showcategory',[
        'uses'=>'CategoryController@create',
        'as'=>'admin.showcategory'
      ]);

[ads]
and for displaying category controller code is here



  $allcategories = Category::all();
        return view('admin.category.updatecategory')->with('categories',$allcategories);

[ads]
Lets see the code for list of category in view ,To check how edit button is working.





 foreach ($categories as $value) {
              $categorySingle = $value['category'];
              ?>
              
                  {{ $categorySingle }}
                  
                      
                        Edit
                      
                  
                  Delete
                      
                  
              
              

[ads]
In this code Foreach loop getting all categories and there is route set with name. We defining route as



      //getting single category
      Route::get('/post/editcategory/{name}',[
        'uses'=>'CategoryController@edit',
        'as'=>'admin.editcategory'
      ]);

And in CategoryController there is a function "edit" which is further powering a view with parameter "name".Here name is category name.





       public function edit($id)
    {
          return view('admin.category.editcategory')->with('name',$id);
    }

[ads]
Now in "editcategory.blade.php" view file we have a form to update category using Ajax.



              
{{csrf_field()}}

[ads]
Above form have been taken using PUT method and action define as route name. We are using token for security reasons.Form looks like this.





How to update data using ajax in laravel 5.8 [ Updated 2019 ]



This form updating category using ajax ,Route defined as given below.





//updating single category 
      Route::put('/upcategory/{name}',[
        'uses'=>'CategoryController@update',
        'as'=>'admin.upcat'
      ]);

[ads]
Ajax code that is updating category is



        $("#upcatsub").click(function(e){
             e.preventDefault();

            if(upcategory == '' ){
              $(".output").html("Field Is Empty");
            }else{

                          var upcategory = $("#upcat").val();
                          var uptoken = $("#uptoken").val();
                          var id = $("#previous_name").val();
                          var url = '/upcategory/'+id;

                           $.ajax({
                                type: "PUT",
                                //url: "{{ url('/post/upcategory') }}",
                                data: {'category':upcategory,
                                       '_token': uptoken
                                      },
                                url:url,
                                success: function(data) {
                                     $(".output").html(""+data+"")
                                },
                                error: function(data) {
                                    $(".output").html(""+data+"");
                                }
                            });
           }
    });

[ads]
On click we are disabling the submit and processing using ajax the "CategoryController" controller having function "update()" is updating data in database and code for it is here





  public function update(Request $request, $id)
    {
        
      $categoryNew = $request->input('category');
      DB::table('categories')
          ->where('category', $id)
          ->update(['category' => $categoryNew]);

      echo "Data Updated Successfully";
    }

[ads]
Thats it it is working just fine.If You are facing problem write to us or open topic in Forum Here