dimanche 28 mai 2017

How to create a condition inside the destroy method in controller in Laravel

I have this problem. In my dashboard I want to Deactivate and Activate Account for my Registered Users and Admin

here's my controller:

public function destroy($id)
{
   $getVal = Input::has('status');
    if($getVal === false) //if checkbox is set to false
    {
        $this->users->softDel($id);
    }
    else
    {
        //if checkbox is set to true
        $this->users->restoreDel($id);
    }
    return redirect('\users');
}

Model:

public function softDel($id)
{
     return User::where('id',$id)->delete();
}
public function restoreDel($id)
{
    return User::where('id',$id)->restore();
}

blade:

{!! Form::open(['method' => 'DELETE' , 'route' => ['users.destroy',$data->id]]) !!}
    <!--checkbox-->
    {!! Form::checkbox('status',null,( $data->deleted_at != null ? false : true ),['data-toggle' => 'toggle', 'data-size' => 'small','data-on' => 'activate' , 'data-off' => 'deacticvate' , 'data-offstyle' => 'danger' , 'data-onstyle' => 'success', 'class' => 'status' , 'data-id' => ($data->id)]) !!}
    <!---->
{!! Form::close() !!}

here's my ajax request:

jQuery(function($){
   $('.status').on('change',function(){
      var currentToken = $('meta[name="csrf-token"]').attr('content');//getting the token
      var id = $(this).data('id');

        $.ajax({
          method: "DELETE",
          url: "/users/"+id,
          dataType: "json",
          data:  { _token: currentToken},
           success:function(response)
            {
                console.log(response);
            }

       });
    });
});

I'm only using soft deletes to delete/restore any account.. My controller will only execute the IF statement. I don't know what happened. Please help :) thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire