Laravel, increase the default fields of User authentication, already ready in the Windows via Poser

Asked

Viewed 1,294 times

0

Hello I wanted to increase the number of fields of the authentication form to insert new fields.

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('users');
}
}

I wanted to put more fields to insert via form, both validation and Insert.

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->string('senha');
        $table->string('endereco');
        $table->string('cidade');
        $table->string('telefone');
        $table->string('razao_social');
        $table->string('cnpj')->unique();
        $table->rememberToken();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('users');
}
}

Can someone help me tell me where these camps are?

  • There is a however, before or after running the first Migration of users has differences in commands!

  • related: http://answall.com/a/165058/54880

1 answer

3


To add extra fields to the default Laravel user, make the following changes:

database/Migrations/xxxx_xx_xx_xxx_create_users_table.php

Schema::create('users', function (Blueprint $table) {
    ...

    $table->string('phone')->nullable();

    ...
}

Now, update your template to track Migration changes, add within the fillable property the new attributes.:

app User.php

class User extends Authenticatable
{
    ...

    protected $fillable = [
        'phone',
    ];

    ...
}

Now you must change the validator within the controller authentication standard, let’s add a simple validation to the new property within the method validator. Look how it turned out:

app Http Controllers Auth Authcontroller.php

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',
        'phone' => 'string|min:8|max:13',
    ]);

    ...
}

Also add the property within the method create in the same file:

protected function create(array $data)
{
    ...

    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'phone' => $data['phone'],
    ]);

    ...
}

Finally, you should add the new field inside your html code however you like, but with the same name you added in the method create of controller. The standard login and registration forms are usually located at resources/views/auth/login.blade.php and resources/views/auth/register.blade.php.

The best thing to do is to read the authentication page on official documentation of Laravel, is very simple and detailed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.