切换数据库中的数据

I am trying to toggle a switch to set my database status to either a 0 or 1. I am using an ajax request but nothing happens when i try toggle. What am i not doing right?

I have included an update but nothing happens and there no errors in my console as well. Am i doing the right thing?

Controller

 public function updateFlag()
    {
        if(Request::ajax())
        {
            $auto_reply = Toggle::where('id', Auth::user()->id)->first();
            if($auto_reply->status == 0)
            {
                $auto_reply->status = 1;

                $auto_reply->save();
            }

            else{
                    $auto_reply->status = 0;

                    $auto_reply->save();
                }
        }
    }

HTML

<div class="container ">
    <input checked data-toggle="toggle" data-on="Enabled" data-off="Disabled"  name="auto_reply" type="checkbox">
</div>

JS

<script>
$("[name='auto_reply']").bootstrapSwitch();
        $(document).ready(function() {
            $("[name='auto_reply']").on('switchChange.bootstrapSwitch',function(){
                $.ajax({
                    url: '/admin/dashboard/status/update',
                    method: 'post',
                    success: function(data){
                        alert("Auto Reply has been Turned On");
                    },
                    error: function(){
                        alert("Auto Reply has failed");

                    }
                });
            });
        });
</script>

Route

Route::post('admin/dashboard/status/update', 'StatusController@updateFlag');

Update

<script>

        $(document).ready(function() {
            $("[name='auto_reply']").on('switchChange.bootstrapSwitch',function(){

                $.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
    }
});
                $.ajax({
                    url: '/admin/dashboard/status/update',
                    method: 'post',
                    success: function(data){
                    alert("Auto Reply has been Turned: "+data.msg);
                    },
                    error: function(){
                    alert("Auto Reply has failed");

                    }
                 });
            });
        });
</script>

Error log

[

    2017-11-19 19:48:23] local.ERROR: Non-static method Illuminate\Http\Request::ajax() should not be called statically {"userId":1,"email":"data@gmail.com","exception":"[object] (ErrorException(code: 0): Non-static method Illuminate\\Http\\Request::ajax() should not be called statically at /Users/emmnock/FastFoodOderSystem/app/Http/Controllers/Admin/LoyaltyController.php:70)
    [stacktrace]
    #0 /Users/emmnock/FastFoodOderSystem/app/Http/Controllers/Admin/LoyaltyController.php(70): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(8192, 'Non-static meth...', '/Users/emmnock/...', 70, Array)
    #1 [internal function]: App\\Http\\Controllers\\Admin\\LoyaltyController->updateFlag()
    #2 /Users/emmnock/FastFoodOderSystem/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
    #3 /Users/emmnock/FastFoodOderSystem/vendor/laravel/framework/src/Illuminate/R

outing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('updateFlag', Array)

1个回答


尝试一下... </ p>

 公共函数updateFlag(Request $ request)
     {
         if($ request-> ajax())
         {
             $ auto_reply =切换:: where('id',Auth :: user()-> id)-> first();
             if($ auto_reply-> status == 0)
             {
                 $ auto_reply->状态= 1;

                 $ auto_reply-> save();
             }

             其他{
                     $ auto_reply->状态= 0;

                     $ auto_reply-> save();
                 }
         }
     }
</ code> </ pre>
     </ div>

展开原文

原文

Try this...

public function updateFlag(Request $request)
    {
        if($request->ajax())
        {
            $auto_reply = Toggle::where('id', Auth::user()->id)->first();
            if($auto_reply->status == 0)
            {
                $auto_reply->status = 1;

                $auto_reply->save();
            }

            else{
                    $auto_reply->status = 0;

                    $auto_reply->save();
                }
        }
    }

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问