How to get first name and last name from facebook? - laravel

I have tried implementing first name and last name of the person trying to authenticate from facebook. I have seen lots of tutorials and followed them accordingly but couldn't get it.
I have tried
$user = Member::create([
'first_name'=> $providerUser->getFirstName(),
'last_name'=>$providerUser->getLastName(),
'email' => $providerUser->getEmail(),
'profilepic'=>$providerUser->getAvatar(),
'password' => md5(rand(1,10000)),
]);

I assumed the provider you use is socialite, to get last name ans first name you can use $providerUser->user['first_name'] and $providerUser->user['last_name']
So based your code:
$user = Member::create([
'first_name'=> $providerUser->user['first_name'],
'last_name'=>$providerUser->user['last_name'],
'email' => $providerUser->getEmail(),
'profilepic'=>$providerUser->getAvatar(),
'password' => md5(rand(1,10000)),
]);

Related

Making an OAuth request with Laravel for Stripe

I was translating following PHP code:
$person = \Stripe\Account::createPerson(
'{{CONNECTED_ACCOUNT_ID}}', // id of the account created earlier
[
'person_token' => $token,
]);
To:
$making_user = $stripe->account()->persons()->create(
$request[0], // id of the account created earlier
[
'person_token' => $request[1],
]);
Above Laravel code works find without any issue. Does anyone have any idea that what can be the equivalent Laravel syntax of the following:
$response = \Stripe\OAuth::token([
'grant_type' => 'authorization_code',
'code' => 'ac_123456789',
]);
I'm using following as equivalent, but it is giving me error of invalid method "oauth"
$making_account = $stripe->oauth()->create([
'grant_type' => $request['code'],
'code' => 'ac_123456789',
]);
I'm not finding anywhere its syntax anyone have an idea what will be the Laravel syntax of making Oauth call?
This worked for me.
$stripe = new \Stripe\StripeClient('SECRET_KEY_HERE');
$response = $stripe->oauth->token([
'grant_type' => 'authorization_code',
'code' => 'CODE_HERE'
]);

How to associate from a parent model to another

$user = User::create(['name' => request('name'),
'email' => request('email'),
'password' => bcrypt(request('password')),
'place_id' => request('place_id'),
'role_id' => request('role_id') ,
'status' => request('status')]);
if (request('role_id') === 3) {
$user->courier()->associate($user);
$user->save() ;
}
I want to insert in my couriers table the user_id of that inserted user if it has role_id of 3. I tried this but didn't work.
If you want to associate the user with a particular courier, there must be the corresponding courier. First of all, you have to find it or create a new courier. If you have id of the courier then you can try the following.
$courier = Courier::find($user->id);
$courier->user()->associate($user);
$courier->save();
The above code will update user_id field of the courier with courier_id with the id of $user

Laravel, Forcing A Unique Rule To Ignore A Given ID

I am following the instructions from the Laravel Documentation but I can't seem to make the rule work. Basically, I want to add a unique rule for both email and username fields for updating the user profile, but it doesn't seem to work. Am I missing something?
Here is the function for updating user credentials
public function updateCredentials(Request $request, User $user)
{
Validator::make($request->all(), [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'username' => [
'required','string','min:4','max:255',
Rule::unique('users')->ignore($user->id),
],
'email' => [
'required','string','email','max:255',
Rule::unique('users')->ignore($user->id),
],
'asdf' => 'required',
'type' => 'required',
])->validate();
}
Thanks in Advance!
If you want to update user profile,without changing his id,you dont need to set specific rule for that.
Without explicitly setting the new user id,it should stay the same.
You can just name the data you want to change like this:
$user->name = request('name');
$user->lastname = request ('lastname);
$user->email = request('email');
...
$user->save();
session()->flash('message','Profile update complete!');
return redirect('/home');
Doing this the data you want will update,but the id will stay the same.
Seems to be some bug.
Adding filed name worked for me:
Rule::unique('users')->ignore($user->id, 'users')

Laravel 5.5 Form Validation Request unique Rule no Data

Currently updating Laravel from v5.2 to 5.5 and can't figure out how I get the "unique" rule working on update.
This is my rule that works nice on Laravel 5.2
public function rules()
{
$retVal = [
'username' => 'required|alpha_dash|min:4|max:30',
'password' => 'min:6|confirmed',
'password_confirmation' => 'min:6',
'email' => 'required|email|unique:users,email,' . $user->id,
'roles_list' => 'required',
];
if (stristr($this->get('username'), '#'))
{
$retVal['username'] = 'required|email';
}
return $retVal;
}
But now in Laravel 5.5 I get always an "email unique"-error on update. When I type in the user-id manually it works. Like described in the Laravel docs it should work:
https://laravel.com/docs/5.5/validation#form-request-validation
I have no clue how to get the $user object accessible in my form request.
Thank you very much in advance for assist !
Your $user variables seems null.
If you're trying to retrieve the currently authenticated user in your Request class, you can try this:
$user = $this->user();
Next, you have to change your email rules a bit:
'email' => 'required|email|unique:users,email,' . ($user ? $user->id : 0),

Password with FirstOrCreate

I want to use FirstOrCreate for a new user.
Like that:
$user = User::FirstOrCreate([
'name' => $request->username,
'email' => $request->email,
'password' => User::generatePassword()
]);
generatePassword() just generate a random 8 chars string string.
Thing is is doesn't work because it's looking for a user that has this password value.
So, it works when there is no user with this email, but when there is it gives me a constraint error.
What should be the cleanest way to fix it???
You've made a grammatical error. ::firstOrCreate searches based on criteria provided, and if it's not found, it will create the database entry and return the model with that data. ::firstOrNew does that without saving the model automatically.
So, you would want this.
$user = User::firstOrNew([
'email' => $request->email,
]);
We do not include name or password because we are not checking to see if Josh with josh#stackoverflow.com using password foobar123 exists, we just want to know if josh#stackoverflow.com has an account.
Your controller logic seems a bit weird because we would first want to validate that information before creating a model, but I'll roll with it.
$user = User::firstOrNew([
'email' => $request->email,
]);
// This model does not have a DB record.
if (!$user->exists)
{
$user->name = $request->username;
$user->password = User::generatePassword();
$user->save();
}
return $user;
With that logic, we find a record based on email. If the record exists, we pass it. If it does not, we assign it a username and generate a password for it before creating the record and then pass it.

Resources