使用laravel进行Ajax更新

时间:2018-04-17 10:40:52

标签: javascript jquery html ajax laravel

您好我的更新存在此问题,所以这是我的刀片代码:

 <div class="form-group floating-label" id="role_colaboration1">
   <select name="{{$role}}[]" id="role"
           class="form-  control">
    <option value="2">Co-author</option>
    <option value="3">Contributor</option>
    <option value="4">Guest</option>
    </select>
  </div> 

这里我有我的ajax代码

 $(document).ready(function(){ 
     $("#role").change(function(){
         var role_value = $(this).val();
            var bank_id =<?php echo json_encode($bank_id); ?>;

            $.ajax({

                method: "POST",
                url: "{{ url('/banks/change-role-in-bank') }}",
                data: {
                    _token: "{{ csrf_token() }}",
                    role_value: role_value,
                    bank_id: bank_id
                },
                success: function () {
                    $("select[id=role][value=" + role_value + "]");
                },
                error: function () {
                    console.log("error");
                }
            });
       });
    }); 

这是我的路线:

Route::post('/change-role-in-bank', 'BankController@changeRoleInBank');

在这里,我有我的控制器:

 public function  changeRoleInBank(){
       if(request()->ajax()) {
            $bank_id = request()->input('bank_id');
            $role = request()->input('role');

          $bank_invites = BankInvite::select('id')->where('bank_id', $bank_id)->get();
          $changeUserRole = BankInvitedUser::where('bank_invites_id', $bank_invites)->update(['role' => $role]);
            return response()->json(Lang::get('general.bank_deleted'));

      }

它没有更新我做错了什么..?是ajax部分

1 个答案:

答案 0 :(得分:0)

请检查此可编辑代码:

在这里,我有我的控制器:

public function  changeRoleInBank(){
       if(request()->ajax()) {
            $bank_id = request()->input('bank_id');
            $role = request()->input('role');

          $bank_invites = BankInvite::select('id')->where('bank_id', $bank_id)->first();
          $changeUserRole = BankInvitedUser::where('bank_invites_id', $bank_invites->id)->update(['role' => $role]);
            return response()->json(Lang::get('general.bank_deleted'));

      }

我刚刚更改了: $ bank_invites to $ bank_invites-&gt; id

$changeUserRole = BankInvitedUser::where('bank_invites_id', $bank_invites->id)->update(['role' => $role]);