Laravel Auth Check For An Organization - php

when a user tries to register I require them to enter an organization ID, I want that organization ID to be checked against my Organization table and see if it exists. If it exists then register the user and if it fails then return an error message. I've been looking around online and couldn't personally find anything like this. If anybody could help, I'd greatly appreciate it.
I am using Laravel 5.6 with the default auth.
Validator:
return Validator::make($data, [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'org_id' => 'required|string|max:16',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
User Create:
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'org_id' => $data['org_id'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'is_active' => 1
]);

You're looking for the exists rule of Laravel's Validation:
'org_id' => 'required|string|max:16|exists:organizations,id',
The rule is essentially
exists:{table},{column?}
Where table is required, and column is optional, generally used if the name (in this case org_id) is different from the column you want to compare.
For full details, check the Documentation.

Related

Laravel 8.x - Storing data with Eloquent Relationship

I've got a User model that hasOne Membership model, with a users table and a memberships table (each entry in the memberships table has a foreign key linked to a user_id).
I've made a registration page that lets the user have a 7 days trial period on the membership but I'm having trouble storing the data.
This is the dd() of the data in the registration form:
"_token" => "ckRlMligEyTwu7ssOi4TmesycbsPpVQlrJ4jQaBd"
"username" => "JaneDoe"
"password" => "password"
"password_confirmation" => "password"
"expiration" => "2021-04-30"
Now in my controller I've got the following store() method:
public function store(Request $request) {
// validating
$this->validate($request, [
'username' => ['required', 'max:200'],
'password' => 'required|confirmed',
'expiration' => 'required'
]);
// storing
User::create([
'username' => $request->username,
'password' => Hash::make($request->password),
'expiration' => $request->expiration
]);
}
This won't store anything in the memberships table and I have no idea how to correctly write the store method using the Model's Eloquent Relationships declared.
Thanks for the help.
EDIT:
While trying to make some sense i've modified the store() function, now looks like this:
public function store(Request $request) {
// validating
$this->validate($request, [
'username' => ['required', 'max:200'],
'password' => 'required|confirmed',
'expiration' => 'required'
]);
// storing
User::create([
'username' => $request->username,
'password' => Hash::make($request->password)
])->membership(Membership::create([
'expiration' => $request->expiration
]));
}
Now seems like Laravel doesn't know where to get the user_id of the newly created user, like the error suggests:
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `memberships` (`expiration`, `updated_at`, `created_at`)
Your solution is to do:
User::create([
'username' => $request->username,
'password' => Hash::make($request->password)
])->membership()->create([
'expiration' => $request->expiration
]);
Using the relation (membership() not membership as an attribute) will already know the relation key (user_id in this case).
You can see more info about this in the documentation.
Other way of doing same is:
$membership = new Membership([
'expiration' => $request->expiration
]);
User::create([
'username' => $request->username,
'password' => Hash::make($request->password)
])->membership()->save($membership);
More info about save() on the documentation.

Laravel 5.5 email confirmation and model adjustments

I'm using Laravel 5.5 and I want newly registered users to activate their account by confirming their email address. Also, I need some extra fields or name alterations on the existent Laravel User model. name is replaced by first_name and last_name.
I found out that Laravel manages most parts of the registration in the Auth/RegisterController and so I modified what I needed:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:12',
'first_name' => 'required|string|min:2',
'last_name' => 'required|string|min:2',
'terms' => 'accepted'
]);
}
The validator works fine, if I change the first_name min:n, it is correctly thereafter validated and shown to the user [the validation errors].
protected function create(array $data)
{
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'status' => 'inactive',
'password' => bcrypt($data['password']),
'api_token' => static::generateApiKey(),
]);
$activationLink = route('account.activation', static::generateRandomString());
Mail::to($user)->send(new UserAccountConfirmationMail($user, $data['password'], $activationLink));
return $user;
}
However, the creation (create(array $data)) does not work at all. I kind of feel like the code is not even executed (tried to add dd or Log::info('...') in order to find out whether or not it at all is executed) and nothing truly happens when I attempt to register. The page is sort of refreshed, however, no message on the user creation (if successful or not, ...), and subsequently no email in my box.
Am I missing out on some crucial detail here?
One of these days that you have spent hours on finding the errors has just finished. The validator, of which I thought was all fine because it was ordinarily displaying the errors, has been the turning point. I forgot to remove name from the validation. However, this validation error was never shown to me since I only caught these errors on display that I truly needed.
Solution:
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:12',
'first_name' => 'required|string|min:2',
'last_name' => 'required|string|min:2',
'terms' => 'accepted'
]);

Laravel 5.3 check if email exists only if password field is filled in

My application uses the standard validator, and my form makes the user provide an email address. They may continue as a guest, but if they do want to create an account; the only thing they will have to provide is a password and in combination with that email address will create the user account.
However, my issue is I am not sure how to use the validator exists only if the password field has been filled in.
$this->validate($request, [
'first_name' => 'required',
'email' => 'required|confirmed|email',
'last_name' => 'required',
'street_1' => 'required',
'zip_code' => 'required',
'phone_1' => 'required',
'password' => 'required_if:account,1|confirmed',
]);
I could do a check and return redirect with an error message, but I'd prefer to go through the validator if I can.
The simplest solution is to put your validation rules into an array then perform your desired check. So if the user checked the "account creation" checkbox, add the rules.
$rules = [
'first_name' => 'required',
'last_name' => 'required',
'street_1' => 'required',
'zip_code' => 'required',
'phone_1' => 'required',
'password' => 'required_if:account,1|confirmed',
]
if ($request->input('acount') == 1) {
$rules['email'] = 'required|confirmed|email'
}

Validiation check in Laravel php

I'm trying to write a validation check in PHP Laravel for a username field with the functionality to let the user know what went wrong. I have a couple of if statements with regular expression checks but it won't work. The requirements of the regular expression are: can't start with a ".", No more than 1 "." in a row, No capitals, Only a-z, No special characters. So for example like this "user.name" would be valid, but things like "username." or ".username" would all be invalid.
So far I got this:
$oValidator = Validator::make(Input::all(), [
'username' => 'required|regex:/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/',
'username' => 'required',
'password' => 'required',
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required|email'
]);
I want to give feedback for the mistakes that user makes, example: user input is ".username", program feedback should be "Dot in front of string is not allowed".
All you have to do is to include a custom message for your validation.
$this->validate($request, [
'username' => 'required|regex:/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/',
], ['regex' => 'Username cannot start with period(.), etc...]);
Your code should look like this. Please remember regex custom message will apply too all of these fields instead of just username so I would separate username validation like above.
$oValidator = Validator::make(Input::all(), [
'username' => 'required|regex:/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/',
'username' => 'required',
'password' => 'required',
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required|email'
], ['regex' => 'Username cannot start with period, etc...']);

Create record with Relation Laravel 5.1

Hi i have the next code for create my records
Institution::create($request->all());
User::create([
'name' => $request['name'],
'lastname' => $request['lastname'],
'phone' => $request['phone'],
'email' => $request['email'],
'password' => $request['password'],
'state' => 1,
'profile_id' => 1,
'institution_id' => Institution::max('id'),
]);
The last attributes for the User thats correct implement so?
The last 3 user attributes , it is correct to do it that way? or is there a better
Using Institution::max('id') creates a race condition. Since the create() static method of Eloquent::Model returns the newly-created model, you can just do:
$institution = Institution::create($request->all());
User::create([
'name' => $request['name'],
'lastname' => $request['lastname'],
'phone' => $request['phone'],
'email' => $request['email'],
'password' => $request['password'],
'state_id' => 1,
'profile_id' => 1,
'institution_id' => $institution->id,
]);
Creating a record with known parent ids is generally fine if your goal is to minimize the number of database queries and you have the ids of the related models.
Another way to do it, though it triggers more update queries, is to use Eloquent's built-in methods for adding related models. For example:
$institution = Institution::create($request->all());
$state = State::find(1);
$profile = Profile::find(1);
$user = new User([
'name' => $request['name'],
'lastname' => $request['lastname'],
'phone' => $request['phone'],
'email' => $request['email'],
'password' => $request['password']
]);
$user->state()->associate($state);
$user->profile()->associate($profile);
$user->profile()->associate($institution);
$user->save();
However, in this situation, since the related models are not already loaded, and you know their ids, there is no need to fetch them only to associate them with the User.

Categories