Laravel, to increase the default fields of User authentication, already ready in the laravel via composer

0

Hello, I wanted to increase the number of fields in 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 anyone help me to know where these fields are?

    
asked by anonymous 17.06.2016 / 17:33

1 answer

2

To add extra fields to the standard user of laravel, make the following changes:

database / migrations / xxxx_xx_xx_xxxxxx_create_users_table.php

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

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

    ...
}

Now, update your template to follow the migration changes, add new attributes within the fillable property.

app \ User.php

class User extends Authenticatable
{
    ...

    protected $fillable = [
        'phone',
    ];

    ...
}

You should now change the validator within the controller default authentication, let's add a simple validation for the new property within the validator method. Here's how it went:

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 create method to 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 must add the new field within your html code however you want, but with the same name you added in the create method of controller . 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 in the official Laravel documentation , it is very simple and detailed .

    
17.06.2016 / 18:28