I have two tables: profile and users. I tried to code it on RegisterController using the Laravel UI package. However, it didn't work properly. What is the best approach to implement it?
public function register(Request $request)
{
$year = date_diff(date_create($request->birthday),
date_create(date('Y-m-d')));
$profile_data = [
'last_name' => $request->last_name,
'first_name' => $request->first_name,
'middle_name' => $request->middle_name,
'birthday' => $request->birthday,
'age' => $year->format('%y')
];
$profile = Profile::create($profile_data);
return User::create([
'email' => $request->email,
'password' => Hash::make($request->password),
'profile_id' => $profile->id
]);
}
protected function create(array $data)
{
return User::create([
'email' => $data['email'],
'password' => Hash::make($data['password']),
'profile_id' => $data['profile_id']
]);
}
Related
ok, I'm trying to login the users after success registration into their panels. the database line built successfully. but alternately user login become successful but once not.
Here is the Request Controller Code:
$request->merge(['phone'=> '09123456789']);
$request->merge(['name' => 'User']);
$request->merge(['password' => 'pass123456789']);
$request->merge(['email' => 'user#email.com']);
$credentials = $request->validate([
'name' => 'required',
'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/',
'password' => 'required',
'email' => 'required',
]);
User::create([
'name' => $request->name,
'email' => $request->email,
'phone' => $credentials['phone'],
'password' => Hash::make($credentials['password'])
]);
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect()->route('panel');
}
The Model:
protected $fillable = [
'name',
'phone',
'email',
'password',
];
let me know if I didn't describe perfectly
First, I would stop using validation in the controller and use form request validation instead. It's been the standard since Laravel 5.
Try this simplified code using Auth::login() instead:
public function createUser(Request $request)
{
$request->validate([
'name' => 'required|string|max:64',
'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/',
'password' => 'required|string|max:64',
'email' => 'required|email|max:64',
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'phone' => $request->phone,
'password' => Hash::make($request->password),
]);
Auth::login($user);
return redirect()->route('panel');
}
I am trying to add default ROLE upon registration of the user. I add ->assignRole() but it gives me this error
here's my create function
use App\HasRoles;
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
])->assignRole('borrower');
$user_id = $user->id;
// HasRoles::create([
// 'role_id' => 4,
// 'user_id' => $users->id,
// ]);
Referral::find($data['referral_id'])->update ([
'status' => 1,
'date_used' => $data['referral_date_used']
]);
CollectorMember::create ([
'collector_id' => $data['referral_generate_by'],
'borrower_id' => $user_id,
'referral_id' => $data['referral_id'],
]);
return $user;
}
you'll also notice the commented hasRole::create. well I thought that's just like that.
Any suggestions? thanks in advance!
Just add the assignRole() method after create the user.
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$user->assignRole('borrower');
I am working on a school project, and I have a registration form.
I am working in laravel 5 and am using the auth package of laravel.
I added my extra registration forms like this
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:6|confirmed',
'lastName' => 'required|string|max:55|',
'streetAdress' => 'required|string|max:255|',
'houseNumber' => 'required|integer|min:1|',
'city' => 'required|string|max:255|',
'postal_code' => 'required|string|max:255|',
'user_phone_number' => 'required|numeric|min:10|',
'birth_day' => 'required|date|min:1|',
'user_parent_name' => 'required|string|max:255|',
'user_parent_email' => 'required|email|max:255|',
'user_parent_phone' => 'required|numeric|min:10|',
]);
}
That is my validator here is my create
protected function create(array $data)
{
$birthday = explode('-', $data['birth_day']);
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'lastName' => $data['lastName'],
'streetAdress' => $data['streetAdress'],
'houseNumber' => $data['houseNumber'],
'city' => $data['city'],
'user_phone_number' => $data['user_phone_number'],
'postal_code' => $data['postal_code'],
'birth_day_day' => $birthday[2],
'birth_day_month' => $birthday[1],
'birth_day_year' => $birthday[0],
'user_parent_name' => $data['user_parent_name'],
'user_parent_email' => $data['user_parent_email'],
'user_parent_phone' => $data['user_parent_phone'],
]);
}
Here is my html
<input id="user_phone_number" type="number" class="form-control" name="user_phone_number" value="{{ old('user_phone_number') }}" required>
In my database I have a column called user_phone_number. Everything else does store but the user_phone_number is still NULL
Thanks in advance
I was looking through my model and i found the problem. i didnt had the fillable option for user_phone_number,
protected $fillable = [
'name', 'email', 'password', 'lastName', 'streetAdress', 'houseNumber', 'city',
'postal_code', 'birth_day_year', 'user_phone_number', 'birth_day_month', 'birth_day_day', 'user_parent_name',
'user_parent_email', 'user_parent_phone',
];
Was missing user_phone_number
**if you are store phone no in database use this migration work for me
$table->bigInteger('phone');
I want to copy the content of id into owner_id after someone registers.
$table->increments('id');
$table->integer('owner_id');
How do I do this? I have tried this which I obviously expected not to work:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
...
'owner_id' => $data['id'],
]);
}
Since $data only gives you the form information. I am clueless now. Any ideas?
You need to create user first. Only then you can use it's ID:
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$user->update(['ownder_id' => $user->id]);
You can do something like:
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']);
$user->owner_id = $user->id;
$user->save();
First create the user then update the ownder_id by
DB::getPdo()->lastInsertId(); or $user->id.
Try this,
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
User::where('id', $user->id)
->update(['ownder_id' => $user->id]);
return $user;
}
I am trying to add a radiobutton value into my users table. When the user registrate he can choose a "role" so I made 2 radiobuttons like this in my registration form:
{!! Form::radio('role', 'role1', true) !!}<br>
{!! Form::radio('role', 'role2') !!}
This is how I am trying to save the value in my AuthController. Unfortunately it does not work :'(. The role field stays empty and I get no errors.
protected function create(array $data)
{
$role = Input::get('role');
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'active' => 1,
'role' => $role,
]);
}
Anyone knows what I am doing wrong here?
Thanks in advance!!
You need to use $data['role']. This will get the value from the radio buttons.
You can use this:
protected function create(array $data)
{
$role = $data['role'];
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'active' => 1,
'role' => $role,
]);
}
Or you can use this:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'active' => 1,
'role' => $data['role'],
]);
}
And make sure that role is fillable in your User model.
protected $fillable = ['name', 'email', 'password', 'active','role'];
Change statement
$role = Input::get('role');
to
$role = Input::get('role') ? 1 : 0;
Hope this helps!