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', ...);
Related
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'.
I am trying to make the 'name' and 'email' properties guarded in my user model, because I don't want my users to be able to change them after registration.
My user model looks like this:
protected $fillable = [
'province',
'city',
'street',
'postal',
'cellphone',
'facebook',
'instagram',
];
protected $guarded = [
'name',
'email',
'password',
'account_type',
'verified_type',
];
Upon registration, Laravel by default mass assigns these values like so:
//Create the user
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'province' => $data['province'],
'city' => $data['city'],
'street' => $data['street'],
'postal' => $data['postal'],
'cellphone' => $data['cellphone'],
'trial_ends_at' => \Carbon\Carbon::now()->addMonths(3),
'account_type' => $accountType,
]);
But this throws an error for me because 'name' doesn't have a default value and isn't nullable. I understand why I'm getting the error and how to fix it, but I would like to know how I should go about assigning the name and email if they don't have default/nullable properties. For example, something like:
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->save();
$user->update([
//the rest of the mass assignable values
]);
Or is there an easier way?
You can accomplish by adding this to your model.
/*
Attribute which are protected from updating.
*/
protected $protected = [
'name', 'email'
];
protected static function boot()
{
parent::boot();
static::saving(function ($model) {
if($model->id){
foreach($model->protected as $attribute){
$model->$attribute = $model->getOriginal($attribute);
}
}
});
}
Hope the code is self-expresive.
you can use a mutator and remove name from guarded attributes. read the docs here
public function setNameAttribute($newName)
{
if(isset($this->name) && $this->name !== null){
throw new \Exception;
//or do nothing
} else {
$this->attributes['name'] = $newName;
}
}
and do the same for email too
I am pretty much new to laravel(5.2), here i want to concatenate two values say first name and last name during user registration and save it to database, honestly as am new to this whole concept i have very little idea of doing it...it would be great if i could get some help....
User.php
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'first_name', 'last_name', 'email'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
AuthController.php
class AuthController extends Controller
{
protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
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']),
]);
}
}
if you want to save the new value to data base you must register it in you fillable, also to alter the database to accept the new column
protected $fillable = [
'user_name', 'first_name', 'last_name', 'email'
];
protected function create(array $data)
{
return User::create([
'user_name' => $data['first_name'].' '.$data['last_name'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
you may want to remove first_name and last_name.. and store only full name, or you can ignore storing the full name, and in User.php model you can override the toArray() method and make it returns the full name which you can concatenate just like above
Concatenation of two strings in a php concept, it do not relates to laravel in any way. You can easily concatenate two or more string like:
$str = $data['first_name'].' '.$data['last_name']; // concatenate by using space
But this does not help you because you are inserting user data into user table which have first_name, last_name columns. So you have to insert them individually.
// to concat with a space in between use
$str = $data['first_name'].' '.$data['last_name'];
// to concat to one big string without spacing use
$str = $data['first_name'].$data['last_name'];
I have a custom function within one of my models. It looks like this:
public function newWithTeam($data, $team_id = false){
$levels = Permissions::get_levels();
$this->email = $data['email'];
$this->password = bcrypt($data['password']);
$this->username = $data['username'];
$this->save();
$profile = new Profile(['name' => $data['name'],'bio' => $data['bio']]);
$this->profile()->save($profile);
}
Here, you can see I store the email, password and username as object properties, before hitting save()
Instead, I'd like to do this in one line, something like:
$this->store(['email' => $data['email], 'password' => $data['password], 'username' => $data['username']]);
$this->save();
I am aware that the create() method exists, but when I use this, the following line
$this->profile()->save($profile); does not work properly. I think the create() function does not work the same as save() for some reason! Is there any equivalent to the store() function as above?
You can use the fill() method to achieve what you are looking for.
But before using it, you should know a few things.
Laravel models are protected against mass-assignment by security reasons, to use the fill() method you will need to define what properties of your model can be filled using the fillable or the guarded properties.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserModel extends Model
{
protected $fillable = ['email', 'password', 'username'];
public function newWithTeam($data, $team_id = false){
$levels = Permissions::get_levels();
$this->fill([
'email' => $data['email'],
'password' => bcrypt($data['password']),
'username' => $data['username']
]);
$this->save();
$profile = new Profile([
'name' => $data['name'],
'bio' => $data['bio']
]);
$this->profile()->save($profile);
}
}
The fillable property functions like a white-list for mass-assignment. If you want the other way, you can use the guarded property that will function like a black-list. Which means that every column listed within the guarded property will not be available for mass-assignment and everything else will, it's your choice.
About your last statement, if you look at the implementation of the create() method you will find that it accepts a regular php array, while the save() method will accept an Eloquent Model instance. That's why create() will not work receiving your $profile variable.
I hope this helps.
The method that You're looking for is fill:
$this->fill([
'email' => $data['email'],
'password' => $data['password'],
'username' => $data['username']]
);
$this->save();
You can use it for both creating and updating.
You can use fill directly:
$this->fill($data);
$this->save();
It will use the array keys as object attributes, so make sure you'll use the same.
Also, make sure you have set the $fillable in your model (http://laravel.com/docs/eloquent#mass-assignment):
class x extends Eloquent {
protected $fillable = ['email','password','username'];
...
}
I'm not 100 % sure I understood you correctly, but it sounds like you want to make a oneliner update without screwing up the profile creation. In that case, you can simply use the update() method:
$this->update(['email' => $data['email], 'password' => bcrypt($data['password]), 'username' => $data['username']]);
That saves the new values for you.
So what happens when you run $this->create()? Are you getting an error?
Consider that create() will return the new object that you created so you could try:
$user = $this->create([
'email' => $data['email],
'password' => $data['password],
'username' => $data['username']
]);
$user->profile()->create([
'name' => $data['name'],
'bio' => $data['bio']
]);
Didn't test this so see how you get on and leave a comment if you have any further issues. Happy coding!
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'];