I want make register form by number, name, password with Laravel auth.
So I change username method in LoginController.php and validate method ,create method in RegisterController.php like following code.
But always show error
SQLSTATE[HY000]: General error: 1364 Field 'email' doesn't have a default value
LoginController
public function username()
{
return 'number';
}
RegisterController
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'number' => ['required'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'password_confirmation' => ['required'],
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'number' => $data['number'],
'password' => Hash::make($data['password']),
]);
}
I spent 2day for find registration customize authentication but I only see login customize.
I finded only one about registration customize.
But this page don't show solution.
https://laracasts.com/discuss/channels/laravel/user-registration-without-email?page=1
please help me.
Your User Model has email field that it:
requires
does not have default value when you try to create new user.
Go to your migrations create_users_table, and edit:
$table->string('email')->unique();
to
$table->string('email')->nullable();
But this is bad idea in my opinion, how will you identify those users later on? How will you let them reset their passwords?
Related
please I am trying to allow users to register with a coupon code, if coupon code is invalid dont register user, but when I tried it users are been registered even though the code is already used or invalid
I am using this package for the code https://github.com/michael-rubel/laravel-couponables
public function store(Request $request)
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
'username' => ['required', 'string', 'max:255', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);
$user = User::create([
'username' => $request->username,
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
$user->redeemCoupon($request->code);
event(new Registered($user));
Auth::login($user);
return redirect(RouteServiceProvider::HOME);
}
You are already registering the user before checking if the coupon is valid. Move this line after the validation.
$user->redeemCouponOr($request->code, function ($e) {
//handle the different exceptions here if not valid.
});
$user = User::create([
'username' => $request->username,
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
Or handle the validations inside the validator method
$request->validate([
...
'code' =>'sometimes|exists:coupon_table,coupon_code_column,coupon_status_column,!used_or_some_other_status'
]);
Make a custom validation rule to verify code and check if the code is already used.
$redeemer->verifyCoupon($code);
$redeemer->isCouponAlreadyUsed($code);
I don't see anything that is not explained in the Laravel Couponables package that you are using it clearly has explanations on the GitHub:
Listeners
If you go event-driven, you can handle package events:
CouponVerified
CouponRedeemed
CouponExpired
CouponIsOverLimit
CouponIsOverQuantity
NotAllowedToRedeem
FailedToRedeemCoupon
All the exceptions are well explained in the documentation
If something's going wrong, methods verifyCoupon and redeemCoupon will throw an exception:
CouponExpiredException // Coupon is expired (`expires_at` column).
InvalidCouponException // Coupon is not found in the database.
NotAllowedToRedeemException // Coupon is assigned to the specific model (`redeemer` morphs).
OverLimitException // Coupon is over the limit for the specific model (`limit` column).
OverQuantityException // Coupon is exhausted (`quantity` column).
CouponException
You can simply replace this line from your code:
$user->redeemCoupon($request->code);
To this:
$user->redeemCouponOr($request->code, function ($exception) {
// Your action with $exception!
print('This coupon is no longer valid'); //
});
I'm trying to customize Laravel's authentication fields. I succeeded for the "name" and "password" fields, but not for the "email" field. I still have the error:
SQLSTATE[42S22]: Column not found: 1054 "email" field unknown in where
clause.
I tried to rely on this this, but it didn't work. In RegisterController, I changed the create function to the following.
protected function create(array $data)
{
return User::create([
'user_pseudo' => $data['name'],
'user_email' => $data['email'],
'usr_mdp' => bcrypt($data['password']),
]);
}
This error could come from the unique validation of the email field in the validation method. If there's no column name specified it will use the name of the field as the column name.
Add the correct column name to the rule in which it's supposed to search for the email and this error should be gone:
RegisterController.php
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,user_email'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
I have added a column for api_token and in my register controller , while creating the user I am trying to generate a unique id , but its not generating any code . Here is my create user function in register controller
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'api_token' => md5($data['email'].$data['name']),
'password' => bcrypt($data['password']),
]);
}
Do I need to add this somewhere else?
As cbaconnier mentioned, you also need to add api_token to your $fillable array in the User model.
I am making an web application and I want only those people knowing a special code can register. How should I do it or check the code entered by the user on the registration form..the validator function in RegisterController has this code:- .
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'place' => 'required|max:255',
'password' => 'required|min:6|confirmed',
]);
}
what inclusion should I made.?
In case you want to store the registration codes in a separate table, you can do this in your validator.
codes
id - integer
code - string
Validation logic
'code' => 'required|exists:codes',
If you want to use a custom column name to validate then you can do this
'code' => 'required|exists:codes,columnName',
If the code is a simple hardcoded value then you can write a custom validation rule and use it.
Add this in the boot method of AppServiceProvider
Validator::extend('registration_code', function ($attribute, $value, $parameters, $validator) {
return $value === 'supersecretcode';
});
Validation logic
'code' => 'required|registration_code,
You can use this if you are using a controller !!!
public function validator(Request $request)
{
// validation
$this->validate($request, array(
'title' => 'required|max:255',
'slug' => 'required|alpha_dash|min:5|max:255|unique:posts,slug',
'body' => 'required'
));
I use the standard mechanism authentication in Laravel 5.3 is built by command:php artisan make:auth.
So, It have created controller RegisterController with the following methods:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]));
}
So, I need to extend this method that to add data to related table for User table. For this I do:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
])->roles()->attach(Role::where('name', "admin"))->first());
}
After this I get error:
FatalThrowableError in SessionGuard.php line 441:
Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in \vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 32
I didn't use Laravel's auth logic, but it looks like the user gets logged in right after they're created. To log the user in Laravel needs the User object, hence:
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
The create() method returns Model. You, however, added attach() to the return statement, and if you take a look at the API, the method returns void which eventually translates to null mentioned in the error message.
What you want to do, is something like this:
protected function create(array $data) {
// Create the User and store the object
$newUser = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
// Add extra data
$adminRole = Role::where('name', 'like', 'admin')->first();
$newUser->roles()->attach($adminRole->id);
// Return the new User object
return $newUser;
}
Note: I assumed you have an id column in your role(s) table.