weixin_33682719 2017-11-19 20:14
浏览 28

切换数据库中的数据

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条回答 默认 最新

  • weixin_33734785 2017-11-20 02:49
    关注

    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();
                    }
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题