I'm trying to register a class of users (customers), and I'm currently hitting this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause' (SQL: select count(*) as aggregate from `users` where `name` = john123)
The column does exist in the database table 'customers', additionally this SQL query laravel has generated looks odd? Register Controller:
protected function createCustomer(Request $request)
{
// dd($request->all());
// $this->validator($request->all())->validate();
$validator = Validator::make($request->all(), [
'name' => ['required', 'alpha_dash', 'string', 'max:25', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:6', 'confirmed'],
]);
if ($validator->fails())
{
$messages = $validator->messages();
return Redirect::back()->withErrors($validator)->withInput();
foreach($errors->all() as $error) {
echo $error;
}
}
elseif ($validator->passes())
{
$customer = customer::create([
'name' => $request['name'],
'email' => $request['email'],
'password' => Hash::make($request['password']),
]);
return redirect()->intended('login/customer');
}
}
Routes:
Route::post('/register/customer', 'Auth\RegisterController@createCustomer')->name('customer.submit');
register.blade.php:
<form method="POST" action='{{ route("$url.submit") }}' aria-label="{{ __('Register') }}">
<p>{{ route("venue.submit") }}</p>
<!-- <p style="position: relative; bottom: 50px;">{{ $url }}</p> -->
@foreach($errors->all() as $error)
<div>
{{$error}}
</div>
@endforeach
@else
<form method="POST" action="{{ route('venue.submit') }}" aria-label="{{ __('Register') }}">
@endisset
@csrf
...etc
Users:
protected function validator(array $data)
{
return Validator::make($data, [
'username' => ['required', 'alpha_dash', 'string', 'max:25', 'unique:users'],
'fname' => ['required', 'alpha', 'string', 'max:15'],
'lname' => ['required', 'alpha', 'string', 'max:15'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
// $profile = new profile();
// $profile->user_id = $data[''];
return User::create([
'username' => $data['username'],
'lname' => $data['lname'],
'fname' => $data['fname'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
Any help would be so awesome! Thankyou :)
You are validating unique in your users table which doesn't have the name column. So its giving you the error. Use customers table instead.
$validator = Validator::make($request->all(), [
'name' => ['required', 'alpha_dash', 'string', 'max:25', 'unique:customers'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:customers'],
'password' => ['required', 'string', 'min:6', 'confirmed'],
]);