Laravel 5.1 create user won't insert added column? - php

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'];

Related

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'.

Create customer on laravel registration

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.

How to insert into another table after Registration on Laravel?

So, I have a laravel web and have some tables, two of them are users and user_data.
user_data have a foreign key user_id that references to id on users.
users have general attributes like username, email, password, etc. and user_data have 1 on 1 relation with users and have attributes like game_money, game_job, game_tier, etc.
The columns are too long if I combine those 2 into 1 table, so I tought to normalize it.
User registration is working and running smooth. But I don't know how to add a new entry into user_data when a new user registered and add a new entry in users.
The attributes columns in user_data (like game_money,etc.) are filled by external application outside laravel, so all I need is to add an entry to user_data.user_id foreign key, and let the other attributes in user_data use the default values or null before being updated by the external apps.
Here is my user_data migration:
Schema::create('user_data', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('account_tier')->default("Free");
$table->float('economy')->default(0);
$table->string('job')->nullable();
$table->timestamps();
});
Where should I put the insert query inside laravel? Or should I handle user_data using the external app?
Thank you very much.
You should use an id on user_data table as your primary key. By the way you can just use the following code for your desired result.
In the registration method use something like below:
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
The user variable is holding the information of the newly created user. Now its time to insert into your user_data table. Assuming the model is UserData.
UserData::create([
'user_id' => $user->id,
............
]);
In RegisterController
inside create function
$user= User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
//UserData is the Model of user_data table
$user->UserData->create(['job'=>$data['job']]);
return $user
Try To This Example:
use Illuminate\Support\Facades\DB;
$user_data= array(
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
);
$role_data= array(
'user_name' => $data['name'],
'email' => $data['email']
);
$response = DB::table('users')->insert($user_data);
$response = DB::table('role')->insert($role_data);
You can use the Model boot events as following when you define relationship each other these models;
public static function boot()
{
parent::boot();
self::created(function($model){
$model->userData()->create([
'user_id' => $model->id,
'job' => $model->job
]);
});
}

How to write a text into one database column for every new user during registration in Laravel 5.2?

I'm using Laravel 5.2 Auth feature and registration works fine.
Now I need to write a string in a special column in users table during registration. It's not a value from an HTML form input. It's a static text describing a user role (contributor). All users registering should receive a "contributor" value in user_role column.
I tried to achieve this by adding this line to AuthController.php:
'user_role' => 'contributor',
The whole function adding values to database looks like this:
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'user_role' => 'contributor',
]);
}
When I try to register a new account, everything except 'user_role' => 'contributor', gets added.
What's the proper way to write "contributor" value for every new user into the user_role column?
Is it not working because that function only has the $data array set?
Did you add user_role to $fillable array on User model ?
If not you need to add it
Example
protected $fillable = ['name', 'email', 'password', 'user_role'];

Laravel 5 two questions

I'm laravel newbie and I have 2 questions:
1. How to set "From:" who in password reminder sent? Because nothing prints now:
2. How to insert custom value when registering?
I'm tried in AuthController but 0 emotions, nothing inserts:
Default:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
I' tried but not works:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'seo_name' => str_slug($data['name']),
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
But nothing happened. Why? Thanks in advance ;)
P.S I have seo_name row in PhpMyAdmin and I want when registration successfull insert for example:
name - Custom Name
seo_name - custom-name (str_slug function makes that string, but IDK how to insert..)
How to set "From:" who in password reminder sent? Because nothing prints now
In Laravel, you can make use of the SwiftMailer library by using the Mail class. This will give you access to commands such as $message->from('youremail#emailaddress.com', 'Firstname Lastname');
Here's an example -
Mail::send('emails.emailview', $data, function($message) {
$message->from('youremail#emailaddress.com', 'Firstname Lastname');
$message->to('to#emailaddress.com')->cc('anotherperson#emailaddress.com');
});
How to insert custom value when registering?
You'll have to update your User model with the new field in the $fillable property. This should reflect the name of the table that you've added for the new field.
class User extends Eloquent {
protected $guarded = array('id', 'password');
protected $fillable = array('name', 'seo_name', 'email');
}
for inserting the custom value the above mentioned method $fillable is required.Put $fillable on to your model and add the fields are to be needed to insert.
eg.
$this->db->table('tb_name')->create([name => $value,
address => $val,
...
]);
then you add protected $fillable = array('name', 'address', ...);

Categories