donglin1192 2018-05-01 23:59
浏览 105
已采纳

如何提交切换数据以通过Ajax更改我的mysql中的布尔值的状态?

I would like to change the status of my boolean in my msql5.7 when I click a button in my view.

I want that when I click on my button, the value of my boolean is switched( toggled between 0 and 1)

Here is my ajax code (my Dashboard.blade.php)

function reply_click(clicked_id){
    $(function() {
        $('#openButton').on('click', function(data) {
            $.get("test.php", {clicked_id);
        });
    });
}

my DashboardController.php

class DashboardController extends Controller
{   
    public function index() {  
        return view('dashboard');
    }
}

my migration file

public function up()
{
    Schema::create('chaves',function (Blueprint $table){
        $table -> increments('id');
        $table -> string('nome');
        $table -> boolean('alugado');
        $table -> timestamps();
    });
}

my route

Route::get("dashboard", "DashboardController@index")->name("dashboard")->middleware('auth');
  • 写回答

2条回答 默认 最新

  • dongliu8542 2018-05-03 02:02
    关注

    JQuery

    function reply_click(clicked_id){
            $(function() {
                $.ajaxSetup({
                   headers: {
                      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                   }
                });
                $('#openButton').on('click', function(data) {
                    $.ajax({
                      type: "PATCH",
                      url: "/dashboard"
                    })
                    .done(function(data){
                        console.log(data);
                    })
                    .fail(function(data){
                        console.log('Error:', data);
                    });
                });
            });
        }    
    

    Controller

    use App\Chave; //use this if you have model
    use Illuminate\Support\Facades\DB; //use this if you don't have model
    class DashboardController extends Controller
    {   
        public function index() { 
            $dashboard = Chave::first(); //use this if you have model
            $dashboard = DB::table('chaves')->first();
            $dashboard->alugado = 1;
            $dashboard->save();
            return view('dashboard');
        }
    }
    

    Router

    Route::patch("dashboard", "DashboardController@index")->name("dashboard")->middleware('auth');
    

    I think that you have to use method Patch and not using Get in your router.. because in this case, you are trying to do update but not update all..

    Put method is used to update partial resource

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建