I do not know English much.
I want to register a member with 2 steps but I get an error
I am getting error different from type 1
Error : SessionGuard::login() must be an instance of
protected function create(array $data)
{
if ($data['type'] == 0){
$user = User::create([
'type' => 0,
'name' => $data['name'],
'gender' => $data['gender'],
'email' => $data['email'],
'phone' => $data['phone'],
'password' => Hash::make($data['password']),
]);
$user->assignRole('User');
return $user;
} else {
// Redirect to 2 step form.
}
}
Step 2 Form:
<form>
... bla bla bla
</form>
Step 2 create function :
protected function create(array $data)
{
$user = User::create([
'type' => 1,
'name' => $data['name'],
'gender' => $data['gender'],
'email' => $data['email'],
'phone' => $data['phone'],
'password' => Hash::make($data['password']),
]);
}
Help me please...
Related
I'm getting this error on Laravel Auth register
Call to a member function validate() on null
The error only happens when the validation passes, when it fails it shows returns to the register view with the correct errors.
I tried to dd after the if fails but it doesn't reach it:
protected function validator(array $data){
$req = new Request($data);
//dd($req,$data);
$this->validate($req,
[
'name' => ['required', 'string', 'max:50','min:3'],
'email' => ['required','email', 'unique:users'],
'password' => ['required', 'min:8', 'confirmed'],
'uni' => ['required'],
'city' => ['required'],
],[
'required' => 'هذا الحقل مطلوب',
'email'=>'نمط البريد الالكتروني غير صحيح',
'min'=>'يجب إدخال 8 حروف عالأقل',
'email.unique' => 'هذا البريد الالكتروني مستخدم',
'confirmed'=>'الرجاء التأكد من كلمة المرور',
'max'=>'50 حرف هو أقصى حد يمكن إدخاله',
'name.min'=>'الاسم قصير جدا',
]);
}
protected function create(array $data){
if ($this->validator($data)->fails()) {
return Redirect::back()->withErrors($this->validator($data))
->withInput();
}
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'photo'=>'img/user.jfif',
'university'=>$data['uni'],
'city'=>$data['city'],
]);
}
full code of RegisterController
https://pastebin.com/B8XcNbBR
and RegistersUsers (where the stack trace shows the error)
https://pastebin.com/BBTTStLL
Try this one
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:50','min:3'],
'email' => ['required','email', 'unique:users'],
'password' => ['required', 'min:8', 'confirmed'],
'uni' => ['required'],
'city' => ['required'],
],[
'required' => 'هذا الحقل مطلوب',
'email'=>'نمط البريد الالكتروني غير صحيح',
'min'=>'يجب إدخال 8 حروف عالأقل',
'email.unique' => 'هذا البريد الالكتروني مستخدم',
'confirmed'=>'الرجاء التأكد من كلمة المرور',
'max'=>'50 حرف هو أقصى حد يمكن إدخاله',
'name.min'=>'الاسم قصير جدا',
]);
}
protected function create(array $data){
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'photo'=>'img/user.jfif',
'university'=>$data['uni'],
'city'=>$data['city'],
]);
}
I am creating and updating user using below code.
public function store(Request $request)
{
if ($request->ajax()) {
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:users,email,' .$request->user_id,
'password' => 'required',
'role' => 'required',
]);
$user = User::updateOrCreate(['id' => $request->user_id],
[
'name' => $request->name,
'email' => $request->email,
'role' => $request->role,
'password' => Hash::make($request->password)
]);
}
}
When I am updating record Password is updating also. How can I solve the issue ?
How validation is working here 'email' => 'required|email|unique:users,email,' .$request->user_id, when I am creating a record ? Because when I am creating a record at that time $request->user_id is not available.
Im building my onw registration in laravel and when im trying to hash my password i get the error parse_url() expects parameter 1 to be string, array given
//Controller
HomeController.php
$filteredValidation = $request->except('_token');
$password = Hash::make($filteredValidation['password']);
UserRegistrationRequest::create([
'firstname' => $filteredValidation['firstname'],
'lastname' => $filteredValidation['lastname'],
'email' => $filteredValidation['email'],
'year' => $filteredValidation['year'],
'avatar' => $filteredValidation['firstname'],
'buddy' => $filteredValidation['firstname'],
'password' => $password,
]);
//request
UserRegistrationRequest.php
public function rules()
{
return [
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required',
'year' => 'required',
'password' => 'required',
];
}
I have no idea why this is happening
This is the code in the migration:
$table->string('role')->default('Standard');
When I leave the input box blank, it gives me an error:
"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'role' cannot be null
How do we set the default value to "Standard" if the input box is left blank?
Code for Controller
public function store(Request $request)
{
//return ['message' => 'I have your data'];
$request->validate([
'firstname' => 'required|string|max:191',
'lastname' => 'required|string|max:191',
'email' => 'required|string|email|max:191|unique:users',
'password' => 'required|string|min:6',
]);
return User::create([
'firstname' => $request['firstname'],
'lastname' => $request['lastname'],
'email' => $request['email'],
'phone' => $request['phone'],
'role' => $request['role'],
'usernotes' => $request['usernotes'],
'password' => Hash::make($request['password']), //make sure to import Hash: use Illuminate\Support\Facades\Hash;
'created_by' => $request['created_by'],
'updated_by' => $request['updated_by'],
]);
}
In your code $request['role'] should be null which is causing the problem since the role field is not Nullable.
What you can do is, add the dwfault value if the role is null, just made following changes in your code and it should work.
public function store(Request $request)
{
//return ['message' => 'I have your data'];
$request->validate([
'firstname' => 'required|string|max:191',
'lastname' => 'required|string|max:191',
'email' => 'required|string|email|max:191|unique:users',
'password' => 'required|string|min:6',
]);
return User::create([
'firstname' => $request['firstname'],
'lastname' => $request['lastname'],
'email' => $request['email'],
'phone' => $request['phone'],
'role' => $request['role'] ?? 'Standard',
'usernotes' => $request['usernotes'],
'password' => Hash::make($request['password']), //make sure to import Hash: use Illuminate\Support\Facades\Hash;
'created_by' => $request['created_by'],
'updated_by' => $request['updated_by'],
]);
}
That should fix the issue.
Explanation: I am using Null coalescing (??) operator of PHP which will replace the null value with 'Standard'. It works only is PHP 7+, if you have a lower version of PHP then you can consider using the Ternary operator(?:).
Reference: https://www.php.net/manual/en/migration70.new-features.php
use nullable();
$table->string('role')->default('Standard')->nullable();
RegisterController.php
I added here an update function so that when the user wants to login with Facebook, he/she will be redirected to a form and then fill the fields so that their information will be stored in the Database.
protected function create(array $data)
{
if ($data['userEmail']) {
return User::where('email', $data['userEmail'])
->update([
'phone_number' => $data['phone_number'],
'address' => $data['address'],
'country' => $data['country'],
'city' => $data['city'],
'zip_code' => $data['zip_code'],
'state' => $data['state'],
'is_online' => true,
]);
} else {
return User::create([
'full_name' => $data['full_name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'phone_number' => $data['phone_number'],
'address' => $data['address'],
'country' => $data['country'],
'city' => $data['city'],
'zip_code' => $data['zip_code'],
'state' => $data['state'],
'is_online' => true,
]);
}
}
The error when the IF statement returns true is this
"Type error: Argument 1 passed to
Illuminate\Auth\SessionGuard::login() must implement interface
Illuminate\Contracts\Auth\Authenticatable, integer given, called in
C:\xampp\htdocs\esoftwaredeals\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php
on line 35".
However, if it returns false, there will be no error and a new user is created and will automatically redirect to the "/my-account" page which is where I wanted to redirect when the user successfully updated their information.
You need to return User instance from the create() method:
protected function create(array $data)
{
if ($data['userEmail']) {
$user = User::where('email', $data['userEmail'])->first();
$user->update([
'phone_number' => $data['phone_number'],
'address' => $data['address'],
'country' => $data['country'],
'city' => $data['city'],
'zip_code' => $data['zip_code'],
'state' => $data['state'],
'is_online' => true,
]);
} else {
$user = User::create([
'full_name' => $data['full_name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'phone_number' => $data['phone_number'],
'address' => $data['address'],
'country' => $data['country'],
'city' => $data['city'],
'zip_code' => $data['zip_code'],
'state' => $data['state'],
'is_online' => true,
]);
}
return $user;
}
Also, you should use the updateOrCreate() method to keep the code maintainable. For example:
protected function create(array $data)
{
$data['password'] = bcrypt($data['password']);
return User::updateOrCreate(
array_only($data, ['email', 'full_name']),
array_except($data, ['email', 'full_name'])
);
}
An update query returns the number of rows that where affected by the update query.So when the update user is successful it will return 1.
The create method returns the saved model instance. So this is causing the issue in your code.
You can use find and save method provided by eloquent and return the user object in if statement and it will work.
$user = User::where('email', $email)->first();
$user->firstname = $firstname;
$user->lastname = $lastname;
$user->save();
return $user;
Try like this and it should work.
$user = User::find($usuario->ID);
$user->rol_id = 1;
$user->save();
}
}
}
return view('dashboard.index')->with(compact('cantReferidosDirectos', 'cantReferidosIndirectos', 'cantReferidosActivos', 'fechaProxActivacion', 'new_member',
'cantventas', 'cantventasmont', 'fullname', 'rangos', 'cantAllUsers', 'rankingComisiones', 'rankingVentas', 'permiso','noticias', 'contTicket', 'moneda',
'nombreRol'
));
}
/**
* Permite actualizar las informacion de los usuarios
*
* #access public
* #return view
*/
public function ActualizarTodo()
{
$comisiones = new ComisionesController;
$comisiones->ObtenerUsuarios();
$todousers = $this->generarArregloUsuario(Auth::user()->ID);
foreach ($todousers as $user ) {
if ($user['rol'] != 0) {
$activacion = new ActivacionController;
$activacion->activarUsuarios($user['ID']);
}
}
Arguments
"compact(): Undefined variable: fechaProxActivacion"
Como soluciono este error