2

I am submitting data to database but when I hit submit button it gives me the below error.I am using Resource Controller for handling the 7 URLs i.e create , store , show etc . When I submit my Form data it gives me the below mentioned error I don't know what's going on with connection.php but it is giving me that query exception in connection line 669. I hope you people will find a solution.Thanks

Error :

QueryException in Connection.php line 669:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'username' in 'where clause' (SQL: select count(*) as aggregate from `users` where `username` = iftikhar)
in Connection.php line 669
at Connection->runQueryCallback('select count(*) as aggregate from `users` where `username` = ?', array('iftikhar'), object(Closure)) in Connection.php line 629
at Connection->run('select count(*) as aggregate from `users` where `username` = ?', array('iftikhar'), object(Closure)) in Connection.php line 342
at Connection->select('select count(*) as aggregate from `users` where `username` = ?', array('iftikhar'), true) in Builder.php line 1461
at Builder->runSelect() in Builder.php line 1447
at Builder->get(array('*')) in Builder.php line 1808
at Builder->aggregate('count', array('*')) in Builder.php line 1728
at Builder->count() in DatabasePresenceVerifier.php line 58
at DatabasePresenceVerifier->getCount('users', 'username', 'iftikhar', null, null, array()) in Validator.php line 1215
at Validator->validateUnique('username', 'iftikhar', array('users'), object(Validator)) in Validator.php line 416
at Validator->validate('username', 'unique:users') in Validator.php line 356
at Validator->passes() in Validator.php line 381
at Validator->fails() in UsersController.php line 34
at UsersController->store()
at call_user_func_array(array(object(UsersController), 'store'), array()) in Controller.php line 78
at Controller->callAction('store', array()) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(UsersController), object(Route), 'store') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(UsersController), object(Route), object(Request), 'store') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\UsersController', 'store') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
at require_once('D:\xampp\htdocs\laravelking\public\index.php') in index.php line 21

My User Controller:

public function store(){
        $rules = array(
            'username' => 'required|unique:users',
            'email' => 'required|unique:users',
            'password' => 'required|min:5'

        );
        $validator = Validator::make(Input::all(),$rules);

        if($validator->fails())
            return Redirect::to('http://localhost/laravelking/users/create')->withInput()->withErrors($validator);

        User::create(array(
            'name' => Input::get('username'),
            'email' => Input::get('email'),
            'password' => Input::get('password')
        ));

        return 'Inserted';
    }

User VIEW:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Users Data</title>
</head>
<body>
    @foreach($users as $user)
        {!! $user->name !!}
    @endforeach

</body>
</html>

Form :

  <form role="form" method="POST" action="http://localhost/laravelking/users">
                      <input type="hidden" name="_token" value="{{ csrf_token() }}">

                      <div class="form-group">
                        <label for="username">User Name:</label>
                        <input type="username" class="form-control" name="username" id="name">
                      </div>    
                      <div class="form-group">
                        <label for="email">Email address:</label>
                        <input type="email" name="email" class="form-control" id="email">
                      </div>
                      <div class="form-group">
                        <label for="pwd">Password:</label>
                        <input type="password" name="password" class="form-control" id="pwd">
                      </div>
                      <div class="checkbox">
                        <label><input type="checkbox"> Remember me</label>
                      </div>
                      <button type="submit" class="btn btn-default">Submit</button>

2
  • Do you have a username column in your database table 'users' ? Commented Mar 10, 2016 at 15:00
  • @Jilson Thomas, error says there is no username column in the table. Commented Mar 10, 2016 at 15:02

1 Answer 1

2

In your rules, you specified username and when you create a user instance, you are providing name. Double check if you have a username column in your table.

Change this:

$rules = array(
            'username' => 'required|unique:users',
            'email' => 'required|unique:users',
            'password' => 'required|min:5'

        );

To

  $rules = array(
            'name' => 'required|unique:users',
            'email' => 'required|unique:users',
            'password' => 'required|min:5'

        );
Sign up to request clarification or add additional context in comments.

4 Comments

Column name is name thanks for solving that issue but still it is not inserting data into database can you help with that. @Jilson Thomas
In your form, changethe field name username to name. And in the create method: 'name' => Input::get('name'),
it worked like a charm but WHY username is not working as a field name ? Once I did changed field name to name it worked !
Because you are passing all fields from the form to the validator function. So if its username, then the rule cannot look for a field named as name. So to make it easier, all fields and variables should have same name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.