Create customer on laravel registration - php

All I want to do is to create a stripe customer account for every registered user on Laravel.
The problem I'm facing right now is that $new variable isn't passing to User::create function.
I'm trying to edit RegisterController.php, here is create() function I'm using:
public function create(array $data)
{
\Stripe\Stripe::setApiKey('sk_test_XXXXXXXXXXXXXXX');
$new = \Stripe\Customer::create([
'email' => $data['email'],
'name' => $data['name'],
'description' => "Contribee.com platform's user",
]);
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'stripe_code' => $new->id,
'password' => Hash::make($data['password']),
]);
}
I've tested this out in my other controller, and $new outputs generated ID. Everything worked fine.

https://laravel.com/docs/6.x/eloquent#mass-assignment
You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.
Anything not permitted as fillable gets discarded.

Related

LaravelTrust and LaravelAuthUI can't import role to database

I have a problem with LaraTrust $User->attachRole('user'), and Laravel Auth UI. I want to attach a role upon registering a new User. After the registering, I don't see any data in the roles table.
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'phonenumber' => $data['phonenumber'],
'password' => Hash::make($data['password']),
]);
$User->attachRole('user');
}
You are directly returning the created user before attaching the role.
Either:
$user = User::create([]);
$user->attachRole('user');
return $user;
Or
return User::create([])->attachRole('user');

How to obtain the user model's attributes in Laravel via class accessor

I am trying to create a customer at the same time as a user is created in the RegisterController (part of the Laravel Auth package), if that user registers as a customer. However, I have tried adding an 'accessor' to the User class by adding the following code to my User model, to access the user_id property, but the accessor does not seem to work as intended. (SEE: https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor)
public function getUserIdAttribute($user_id)
{
return $user_id;
}
Then I try to access it directly from the model in the RegisterController with this function:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'username' => $data['username'],
'password' => Hash::make($data['password']),
'utype' => $data['utype'],
]);
if ($data['utype'] == "customer") {
Customer::create([
'user_id' => $user->user_id,
]);
}
return $user;
}
For some reason, the $user->user_id returns NULL though. What is the issue? Why is the accessor not being applied properly?
With the help of #lagbox, I figured out what the issue was. Turns out, the column name was simply 'id'.

Assigning user role upon registration in laravel issue

In my laravel application i'm trying to assign user role upon the registration. Following is my RegistrationController
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'mobile'=>$data['mobile'],
'username'=>$data['username'],
'password' => Hash::make($data['password']),
'propic'=>'user-pic.png',
'user_roles' =>'customer',
]);
$lastdata = DB::table('users')->orderBy('id', 'desc')->first();
$last_id=$lastdata->id;
$user= User::where('id','=',$last_id)->firstOrFail();
$user->assignRole('customer');
}
But when I run the code user get created, activation mail has been sent but the role is not assigning! how can i fix that and where am I doing wrong?
There are a few things you might want to consider.
1) your function won't proceed further after the return statement. You are creating a user with a return statement hence your code below of that won't execute. I'll recommend storing the return object into some variable.
// let's say
$user = User::create([
'name' => $data['name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'mobile'=>$data['mobile'],
'username'=>$data['username'],
'password' => Hash::make($data['password']),
'propic'=>'user-pic.png',
'user_roles' =>'customer',
]);
// then you can simply get it's value by
$user_id = $user->id; // you won't need to query last added row's value.
2) If the user role is not getting saved into your database then check if you have assigned that 'user_roles' field as fillable into User model.

How to perform validation when updating a unique field using Http\Request in Laravel

I have a customers table that I use a CustomerRequest to validate the fields, through the use of the rules function. The table has an email field which is required and is unique. This works fine when I create the customer, however when I update the customer info (let's say I got their last name spelled wrong) my request fails because the email address is already in the database.
Here is my CustomerRequest:
public function rules()
{
return [
'givenname' => 'required',
'surname' => 'required',
'email' => 'required|unique:customers,email',
];
}
I would like to reuse the CustomerRequest for all of the customer vaildation, how can I go about doing this?
You need to check here for request type as well as customer id for update and then return rules according to request. Something like this
public function rules(Request $customer_request)
{
return [
'givenname' => 'required',
'surname' => 'required',
'email' => 'required|unique:customers,email,'.$customer_request->get('customer_id'),
];
}

Laravel 5.1 create user won't insert added column?

I have added a column for activation token in my users table and I want to add this random string when inserting new users.
So in my AuthController i have this:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'activation_token' => str_random(60),
]);
}
The user get's registered but the activation_token column remains empty???
What am I doing wrong?
As per the docs, you need to add this new field as a mass assignable field in your User model.
You are likely running in to trouble here as you haven't updated this, and as a result the activation_token is being excluded from the insert.
Try updating your mass assignable fields in your User model to something like this:
protected $fillable = ['name', 'email', 'password', 'activation_token'];

Categories