lundi 28 janvier 2019

Laravel: How to change the specific field value on button click?

I wanted to use ajax to do this but I tried all the references I saw but seems still not working. I'm a beginner of using laravel please help me.

this is my method in my controller

    public function delete(Request $request, $id)
    {
      $validator = Validator::make(Input::all(), $this->rules);
       if ($validator->fails()) {
         $errorMessage = Config::get('const.error.common.missing_required.desc');
         $errorContainerHtml = parent::renderErrorContainerView($errorMessage);
         $response = response()->json(compact('errorContainerHtml'), 400);
       } else {
         try {
            $codes = Model::select ('id', 'status'))
                ->whereId($id)
                ->first();

            if ($codes ->status == 1){
               $codes ->status = 0;
            }else{
               $codes ->status = 1;
            }
            $codes ->save();
            $response = response()->json($codes );
          } catch (\Exception $e) {
            $errorMessage = Config::get('const.error.common.system_error.desc');
            Log::error($e);
            $errorContainerHtml = parent::renderErrorContainerView($errorMessage);
            $response = response()->json(['error' => compact('errorContainerHtml')], 500);
         }    
        }
      return $response;
}

this is my ajax

$('.modal-footer').on('click', '#delete', function() {
        loader.show();
        $.ajax({
            type: 'POST',
            url: '/master/rgbCodes/changeStatus',
            data: "status=" + status,
            success: function(data) {
                 if(data == 1){
                     $(data.status == 0);
                     showAlertSuccess('<b>Successfully added RGB Code information.</b>');
                    window.location.reload();
                } else {
                    $(data.status == 1);
                }
            },
            error: data => {
                showAlertDanger(data.responseJSON.error.errorMessage);
                loader.hide();
            },
            complete: () => {
                loader.hide();
            }
        });
    });

and this is my modal and route

<div id="deleteModal" class="modal fade" role="dialog">
            <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content">
                    <div class="modal-body">
                        <p class="font-weight-bold">Are you sure you want to deactivate this data?</p>
                       <input type="hidden" name="status" id="status">
                    </div>
                    <div class="modal-footer smarts-modal-footer">
                        <button type="submit" class="btn btn-light" data-dismiss="modal" id="delete">Yes</button>
                        <button type="button" class="btn btn-light" data-dismiss="modal">No</button>
                    </div>
                </div>
            </div>
        </div>

Route::post('/master/rgbCodes/status/{status}', 'RgbCodeController@changeStatus')->name('master.rgbCodes.changeStatus');

What I'm planning to do is, when the user click the trash button, confirmation modal will appear if they are sure to deactivate this user. If they click Yes Status in table will be Inactive on the specific row where they click the trash icon.But I can't do it I spent 2 days trying to fix this and keep repeating all the methods that saw in online.

Please don't ignore this..Please. Thank you!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire