简体   繁体   English

向laravel用户模型添加列并保持auth注册行为

[英]Add column to laravel user model and keep the auth register behavior

As many know, Laravel automatically creates the User model when you create a project. 众所周知,Laravel在创建项目时会自动创建User模型。 I then created an auth interface php artisan make:auth . 然后,我创建了一个auth接口php artisan make:auth The auto generated register method was working normaly. 自动生成的注册方法正常工作。 I then added a not null column to the migration for the user table. 然后,我在用户表的迁移中添加了一个非空列。 Now it isn't working anymore. 现在,它不再工作了。 I don't know where exactly the insert happens when laravel creates this auth scaffolding. 我不知道当laravel创建此auth脚手架时,插入发生在哪里。 I tried editing the create method in RegisterController but it didn't worked. 我尝试在RegisterController编辑create方法,但没有成功。

class RegisterController extends Controller
{

protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'tipo' => $data['tipo'], // name of the new column I added
            'password' => bcrypt($data['password']),
        ]);
    }
}

On the register <form> I added the input field tipo . 在寄存器<form>我添加了输入字段tipo The generated code includes: 生成的代码包括:

<form class="form-horizontal" method="POST" action="{{ route('register') }}">

<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>

<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>

<select id="tipo" class="form-control" name="tipo" value="{{ old('tipo') }}" required>
                                <option value="1">Gerente</option>
                                <option value="2">Caixa</option>
                                <option value="3">Garçom</option>
</select>

<input id="password" type="password" class="form-control" name="password" required>

<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>

<button type="submit" class="btn btn-primary">
                                Registrar
</button>

In the User class model, I added the new column to the fillable: 在User类模型中,我将新列添加到可填充项:

/protected $fillable = [
    'name', 'email', 'password', 'tipo',
];

On the routes, web.php only Auth::routes(); 在路由上,仅web.php Auth::routes(); . If I run php artisan route:list it shows me that for the POST method the uri register leads to RegisterController@register but on the RegisterController it has only the create method I posted and validator method. 如果我运行php artisan route:list则会显示出POST方法的uri register会导致RegisterController@register但在RegisterController上,它只有我发布的create方法和validateator方法。 So, where the registration is actually happening? 那么,实际在哪里进行注册? In terms of laravel, where it runs the $user->save() method? 就laravel而言,它在哪里运行$user->save()方法?

Edit: All I want to do is modify it so it include the new <input> for the new column 编辑:我要做的就是对其进行修改,以便为新列包括新的<input>

Edit 2: I found out that actually the code is not reaching the create method because I added dd($data) before the return and still the same behavior, then I added some forced syntax error and also the same thing happened. 编辑2:我发现实际上代码没有到达create方法,因为我在返回之前添加了dd($data)并且仍然是相同的行为,然后添加了一些强制语法错误,并且发生了同样的事情。

Firstly, Add the recently added column to $fillable array in the app\\User.php file. 首先,将最近添加的列添加到app \\ User.php文件中的$ fillable数组中。

<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
    use Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */protected $fillable = [
        'name', 'email', 'password', 'tipo'
    ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM