0

In the document of Laravel 5.2 "Authentication Quickstart":

"Laravel provides a quick way to scaffold all of the routes and views you need for authentication using one simple command".

php artisan make:auth


After run the command, the default behavior will create many files about Model,Controller,View, Database Tables etc…

I found that a "users" table is created, with "id name email password" columns, it's great!

How ever, if my web application has different structure about "user", such as I do not use the unique "email" property to identify.

How can I change the behavior of the command php artisan make:auth?

Thanks!

2 Answers 2

1

Update your migrations as @Taylor Swift said.

As you mentioned. You don't have "email" in your user table.

Update your user migration to the following,

Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); 

AuthController by default uses email to identify users

The laravel AuthController out-of-the box allow users to login using email, password.

Since you don't need the email. (Assuming you're using id as unique value for each users). You need to tell the AuthController to look for users by id rather than email.

So open your AuthController which is in

App\Http\Controllers\Auth\AuthController

and add the following line

protected $username = 'id'; 

Your Auth controller will look like

class AuthController extends Controller { use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $username = 'id'; public function __construct() { $this->middleware($this->guestMiddleware(), ['except' => 'logout']); } } 
Sign up to request clarification or add additional context in comments.

Comments

1

The users table migration is there even before you do php artisan make:auth. So all you have to do to remove the unique key from your email column is to go to the migration file which is located here:

database/migrations/create_users_table.

And to remove the unique key from your email column, simply remove it from code as such

$table->string('email'); 

So it should look like this:

Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email'); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); 

2 Comments

thanks for the answer. But I don't understand, when I modify the migrations php file which is auto create by laravel, drop all the "email" columns. and run the php artisan make:auth. I also get the email relate files, such as some blade.php file . Does it mean I should remove all the email things just auto genreate after run the make:auth command, ?
No, what you need to do is after you remove the unique key from your migration file, you can go to terminal and type in php artisan migrate:refresh and it will refresh the database with the new migration. You don't need to do php artisan make:auth again as it just generates the routes and the views. Your migration file is not generated by that command.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.