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!
Related
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']
]);
}
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');
How to check if user exists in my table or redirect back with a message using Laravel 5.6?
Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Http\RedirectResponse given, called in C:\wamp\www\zainsurgalt\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 35
protected function create(array $data)
{
$isEmailExists = Niigem::where('email', $data['email'])->count();
if($isEmailExists){
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
else{
return Redirect::back()->withErrors(['msg', 'The Message']);
}
}
I Added my Create method here
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Niigem;
use Validator;
use Illuminate\Support\Facades\Redirect;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
protected function create(array $data){
$validator = Validator::make($data, [
'name' => 'required|string',
'email' => 'required|string|email|exists:niigems',
'password' => 'required|string',
]);
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withInput();
}
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
You need to return something when the user is created.
protected function create(array $data)
{
$isEmailExists = Niigem::where('email', $data['email'])->count();
if($isEmailExists){
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
return ........ you need to return something here
} else {
return Redirect::back()->withErrors(['msg', 'The Message']);
}
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|max:255|unique:users|exists:niigems',
'password' => 'required|min:6|confirmed',
]);
}
protected function create(array $data)
{
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
I see your are trying to create a new user if they exist in the niigem table. To do it the Laravel way, you have to validate using Laravel's validation class. So, this should work:
protected function create(array $data)
{
$validator = Validator::make($data, [
'name' => 'required|string',
'email' => 'required|string|email|exists:niigem',
'password' => 'required|string',
]);
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withInput();
}
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
Another way is:
Niigem::firstOrCreate(['email' => 'dummy#domain.com'], ['name'=> $data['name'], 'password' => bcrypt($data['password'])]);
Please use this query you can use both condition as per your need:
$user = Niigem::where('email', '=', Input::get('email'))->first();
if ($user === null) {
// user doesn't exist
}
if ($user !== null)
{
// user doesn't exist
}
I am creating an admin panel where admin can create an user. I have used laravel default registration system. I have modified the RegisterController little. The user is being created successfully. But the problem is after creating user the admin is getting logged out. What is the problem?
Here are my files. I am using laravel 5.3
web.php
Route::group(['middleware' => 'auth:admin'], function () {
Route::get('/admin/user/register','Auth\CreateUserController#showRegistrationForm');
Route::post('/admin/user/register','Auth\CreateUserController#register');
});
CreateUserController
class CreateUserController extends Controller
{
use RegistersUsers;
protected function redirectTo()
{
if(Auth::guard('admin')->check()) return '/admin/index';
else if(Auth::guard('employee')->check()) return '/employee/dashboard';
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'district' => 'required',
'gender' => 'required',
'mobile' => 'required',
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'district' =>$data['district'],
'mobile' => $data['mobile'],
'gender'=>$data['gender'],
]);
}
}
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;
}