1
votes

I have created a project in Laravel but i cant login and register.When i try to register it stays at the same page and all the fields stay the same the password field stays empty.While in Login page it says that "These credentials do not match our records.".I have tried to drop the database,create new and run php artisan migrate or migrate:fresh and refresh but still not working.When i try to enter a user or admin from phpmyadmin it says this error".I dont know if the error comes maybe because department table and user table are joined,foreign key and primary key,with department_id...

1062 - Duplicate entry '[email protected]' for key 'users_email_unique'

Department.php


       public function users()
        {
            return $this->hasMany(User::class);
        }

User.php


public function users()
{
    return $this->belongsTo(Department::class);
}



   public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');//create_users_table
            //$table->text('avatar');
            $table->string('name');
            $table->string('lastname');
            $table->string('phone');
            $table->string('jobtitle');
            $table->integer('department_id');
            $table->timestamps();
            $table->string('usertype')->nullable();
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
           
        });
    }

    public function up()
        {
            Schema::create('departments', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('name');
                $table->timestamps();
            });
        }

RegisterController.php



    class RegisterController extends Controller
    {
       
    
        use RegistersUsers;
        protected $redirectTo = RouteServiceProvider::HOME;
        public function __construct()
        {
            $this->middleware('guest');
        }
    
    
        protected function validator(array $data)
        {
            return Validator::make($data, [
                //'avatar' => ['image', 'mimes:jpeg,bmp,png,jpg', 'max:2048'],
                'name' => ['required', 'string', 'max:255'],
                'lastname' => ['required', 'string', 'max:255'],
                'phone' => ['required', 'string', 'max:10', 'min:10'],
                'jobtitle' => ['required', 'string', 'max:255'],
                'department_id' => ['required', 'string', 'max:255'],
                'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
                'password' => ['required', 'string', 'min:8', 'confirmed'],
                
            ]);
        }
    
    
        
        protected function create(array $data)
        {
    
            return User::create([
                'name' => $data['name'],
                'lastname' => $data['lastname'],
                'phone' => $data['phone'],
                'jobtitle' => $data['jobtitle'],
                'department_id' => $data['department'],
                'email' => $data['email'],
                'password' => Hash::make($data['password']),
            ]);
        }
    }

1
what problem are you facing now?Andy Song
it was solved i forgot to enter 123456 as passsword.thank you very much,you helped me a lot! :) but just one question it doesnt accept the foreach part at dashboardCoder
I really need more details, I can not help you just with a foreach loop. I need you to show me more code, or what error you are facing.Andy Song

1 Answers

3
votes

1062 - Duplicate entry '[email protected]' for key 'users_email_unique' this error means that you have two email addresses are the same in your DB and in your migration, you defined email field as unique. So you get this error.

if the form is not working and you cannot see any errors, that's probably there are some validation errors. Here is how to show all of them.

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif