skip to Main Content

I need without refresh listing page update status of record. I have tried it with full page refresh but I want to an AJAX feature with Laravel rules.

2

Answers


  1. If you like to update a record with Ajax you have to implement Ajax in your template and request a controller method that execute the change.

    jQuery example:

    $.ajax({
        type: "POST",
        url: '/pageController/updateStatus',
        data: {id: 15, status: 'example'},
        success: function(response) { // Change elements in the DOM },
    });
    

    This will post the page ID and status to the updateStatus controller method. On success you can update values in the DOM.

    Controller example:

    class PageController {
        public function updateStatus(int $id, $status) {
            $page = Page::findOrFail($id);
            $page->status = $status;
            $page->save();
    
            return $page->status;
        }
    }
    

    In the controller you search for the given ID in the Page model. Then you update the status, save it, and return the new status so you can use it in the front-end.

    Login or Signup to reply.
  2. Add Jquery in footer

    Add list view page this function

    Add your datatables

    $('#table_user').on('click', '.status_clik', function(){           
     var sid = $(this).attr("sid");
    $.ajax({
        type: "POST",
        url: '/pageController/updateStatus',
        data: {id: sid},
        success: function(response) { // Change elements in the DOM },
    });
    });
    

    In controller page

    class PageController {
        public function status(Request $request){
            $id = $request->get('id');
            if($id!=''){
                $geStatus = DB::table('tablename')->where('id', $id)->first();
    
                $update_data = array('eStatus'=>GET_OPPOSITE_STATUS[$geStatus->eStatus]);
                $update = DB::table('tablename')->where('id', $id)->update($update_data);
                if($update){
    
                    return response()->json(array("status"=>1,"message"=>'Status updated successfully'));
    
                }else{ return response()->json(array("status"=>0,"message"=>_FAIL_TO_PROCESS)); }
            }else{return response()->json(array("status"=>0,"message"=>_FAIL_TO_PROCESS));}
        }
    
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search