我正在向数据库提交数据,但是当我点击提交按钮时,它给出了以下错误。我正在使用资源控制器来处理7个URL,即创建,存储,显示等。当我提交表单数据时它会给我提到下面提到的错误我不知道 connection.php 发生了什么,但它在连接行669中给了我查询异常我希望你们的人能找到解决方案。谢谢
错误:
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
我的用户控制器:
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';
}
用户查看:
<!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 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>
答案 0 :(得分:2)
在您的规则中,您指定了username
,并在创建用户实例时提供name
。仔细检查表格中是否有username
列。
改变这个:
$rules = array(
'username' => 'required|unique:users',
'email' => 'required|unique:users',
'password' => 'required|min:5'
);
要
$rules = array(
'name' => 'required|unique:users',
'email' => 'required|unique:users',
'password' => 'required|min:5'
);