更新用户详细信息jquery模式时检查唯一的用户名

i'm using jQuery modal for user create and update for my laravel project. when creating new user im checking the username availaibilty. but that function doesnot support with update , when user save data without changing username it gives usernam availble error . please advice how to fix

my controller,

public function checkUsernameAvailability()
{
$user = DB::table('users')->where('username', Input::get('username'))->count();

if ($user > 0) {
    $isAvailable = FALSE;
} else {
    $isAvailable = TRUE;
}

echo json_encode(
    array(
        'valid' => $isAvailable
    ));

}


jQuery modal
                        username: {
                            validators: {
                                notEmpty: {
                                    message: 'The Username field is required'
                                },
                                rules : {
                                    username : { alphanumeric : true }
                                },
                                remote: {
                                    url: "{{ URL::to('/checkUsername') }}",
                                    data: function (validator) {
                                        return {
                                            username: validator.getFieldElements('username').val()
                                        }
                                    },
                                    message: 'The Username is not available'
                                },
                                stringLength: {

                                    max: 100,
                                    message: 'The Username must be  less than 100 characters long'
                                }
                            }
                        },

Try this code :

New Code To Be Added

$(document).ready(function(){
    $('#username').data('actval',$('#username').val());
});

Changes In Your Code Javascript

username: {
                        validators: {
                            notEmpty: {
                                message: 'The Username field is required'
                            },
                            rules : {
                                username : { alphanumeric : true }
                            },
                            remote: {
                                url: "{{ URL::to('/checkUsername') }}",
                                data: function (validator) {
                                    return {
                                        username: validator.getFieldElements('username').val(),
-----------------------                     actusername: $('#username').data('actval')
                                    }
                                },
                                message: 'The Username is not available'
                            },
                            stringLength: {

                                max: 100,
                                message: 'The Username must be  less than 100 characters long'
                            }
                        }
                    },

Changes in PHP code

public function checkUsernameAvailability()
{
    $user = 0;
    if(Input::get('username') != Input::get('actusername')){
       $user = DB::table('users')->where('username', Input::get('username'))->count();
   }


if ($user > 0) {
    $isAvailable = FALSE;
} else {
    $isAvailable = TRUE;
}

echo json_encode(
    array(
        'valid' => $isAvailable
    ));

}

1-You can send some parameter in ajax as you know if it is update or new. And you can check it at server side for calling checkUsernameAvailability() based on the parameter value only when it is new user. In the case of update you don't need to call that method.

2- Or don't call ajax if this the case of update for checking user availability.

I think you should check if the username already exist in the table before submitting the form, user .on('blur') of jQuery and call for the function checkusernameAvailability(), then catch the response and after that on the basis of change the function either create user or update user. Hope this will help you.