如何使用按钮更新用户状态而不更新laravel

时间:2019-04-09 12:07:31

标签: laravel

我想更新状态用户,只需传递“状态”参数即可。 这是我的代码,但我不知道,为什么我不能更新用户状态。如何解决这个问题?

我的控制器

public function activation()
{
    // dd('test');
    $action = Input::get('status');
    if (Input::get('activate'))
    {
        $this->activateUser($action);
    }
    elseif (Input::get('inactivate'))
    {
        $this->deactivateUser($action);
    }
    return redirect(route('admins-users.index'))->with('message-post', 'Status was updated successfully');
}

public function activateUser($user)
{
    //dd('active');
    User::findOrNew($user)->update(['status' => "1"]);
}

public function deactivateUser($user)
{
    // dd('nonactive');
    User::findOrNew($user)->update(['status' => "0"]);
}

我的路线

Route::post('admins-users/status', 'Backend\StatusController@activation');

我的观点

{!! Form::open(array('url' => 'admins-users/status')) !!}
       @if ($user->status)
          {!! Form::submit('inactivate', ['name' => 'inactivate', 'class'=>'btn btn-xs btn-default']) !!}
       @else
          {!! Form::submit('Activate', ['name' => 'activate', 'class'=>'btn btn-xs btn-success']) !!}
       @endif
{!! Form::close() !!}

2 个答案:

答案 0 :(得分:0)

尝试一下

  {!! Form::open(['url' => 'admins-users/status', 'method' => 'get' !!}
   <input type="hidden" name="user_id" value="{{ $user->id }}">
   @if ($user->status)
      {!! Form::submit('inactivate', ['name' => 'inactivate', 'class'=>'btn btn-xs btn-default']) !!}
   @else
      {!! Form::submit('Activate', ['name' => 'activate', 'class'=>'btn btn-xs btn-success']) !!}
   @endif
  {!! Form::close() !!}

您的路线

Route::get('admins-users/status', 'Backend\StatusController@activation');

您的控制器方法

public function activation(Request $request)
{
     $user_id = $request->user_id
     if ($request->acticate) {
           $this->activateUser($user_id);
     }
     elseif ($request->inactiavte) {
           $this->deactivateUser($user_id);
     }
return redirect(route('admins-users.index'))->with('message-post', 'Status was updated successfully');
}

希望这会有所帮助:)

答案 1 :(得分:0)

尝试了几种方法之后。最终,我找到了使用Ajax并选择选项来解决此问题的答案。我在传递给控制器​​之前添加哈希ID,然后在控制器中再次解码。我认为必须关注通行证。

我的控制器

public function control(Request $request){
    $status = $request->status;
    $iduser = $request->iduser;
    $key = Hashids::connection('main')->decode($iduser)[0] ??  abort(404);
    $update = User::where('id', $key)->update(['status'=> $status]);
    if($update)
    {
        return redirect(route('admins-users.index'))->with('message-post', 'Status user was updated successfully');
    }
}

我的表格 使用哈希ID编码ID用户

 @php $parameter = Hashids::connection('main')->encode($user->id); @endphp
{!! Form::hidden(null,$parameter, ['id'=> 'iduser'.$parameter ])!!}
{!! Form::select(
                'status',
                array(1=>'Active',0=>'Not Active'),
                $user->exists ? $user->status : null,
                [
                    'id'          => 'action'.$parameter,
                    'placeholder' => 'Choose a status'
                ]
            )
!!}

脚本

$(document).ready(function(){

    @foreach($users as $user)
    @php $parameter = Hashids::connection('main')->encode($user->id); @endphp
    $("#action{{ $parameter }}").change(function(){
        var status = $("#action{{ $parameter }}").val();
        var iduser = $("#iduser{{ $parameter }}").val();
        if(status==""){
            alert("Please select an option");
        }
        else{
            if (confirm('Do you want to change {{ $user->name }} status to {{ $user->status ? 'InActive' : 'Active' }}?')) {
                $.ajax({
                url: '{{ url("/action") }}',
                data: 'status=' + status + '&iduser=' + iduser,
                type: 'get',
                success:function(response){
                    console.log(response);
                }
                });
                document.location.reload();
            }
        }
    });
    @endforeach
});