Laravel显示数据库数据中的不同数据

时间:2017-06-20 21:18:19

标签: php laravel authentication laravel-5 laravel-5.3

我一直在研究某个应用程序。突然之间,我发现了这个奇怪的错误。显示的数据与我在数据库中的数据不同。 这就是我在我的数据库中所拥有的'

id - 1
name - Jon Doe
email - jondoe@gmail.com
status - jobseeker
created_at - 2017-06-20 21:02:47
updated_at - 2017-06-20 21:02:47

但是当我显示数据时,这就是我得到的

{"id":1,"name":"Jon Doe","email":"jondoe@gmail.com","status":"employer","created_at":"2017-06-20 21:02:47","updated_at":"2017-06-20 21:02:47","employer":null}

我正在使用blade" {{Auth :: user()}}"登录后显示用户信息

我认为它与"属性"有关。和"原创"当你破坏数据时。

我不知道发生了哪些变化。我使用Laravel默认身份验证来创建用户。

这是我的创建用户表单..

<form role="form" method="POST" action="{{ route('register') }}">
                  {{ csrf_field() }}

                  <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                    <label>Name</label>
                    <input type="text" class="form-control" name='name' placeholder="Your Name" required>
                    @if ($errors->has('name'))
                        <span class="help-block">
                            <strong>{{ $errors->first('name') }}</strong>
                        </span>
                    @endif
                  </div>

                  <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                    <label>Email</label>
                    <input type="email" class="form-control" name='email' placeholder="Your Email" required>
                    @if ($errors->has('email'))
                        <span class="help-block">
                            <strong>{{ $errors->first('email') }}</strong>
                        </span>
                    @endif
                  </div>

                  <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                    <label>Password</label>
                    <input type="password" class="form-control" name="password" placeholder="Your Password" required>
                    @if ($errors->has('password'))
                        <span class="help-block">
                            <strong>{{ $errors->first('password') }}</strong>
                        </span>
                    @endif
                  </div>
                  <div class="form-group">
                    <label>Re-type Password</label>
                    <input type="password" class="form-control" name="password_confirmation" placeholder="Re-type Your Password" required>
                  </div>

                  <div class="form-group">
                    <label >Register As</label>
                    <select class="form-control" required name="status">
                      <option value="">--select--</option>
                      <option value="jobseeker">Job Seeker </option>
                      <option value="employer"> Employer </option>
                    </select>
                  </div>
                  <div class="white-space-10"></div>
                  <div class="form-group no-margin">
                    <button class="btn btn-theme btn-lg btn-t-primary btn-block">Register</button>
                  </div>
                </form>

这是我的用户模型

class User extends Authenticatable
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password','status'
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

public function employer(){
  return $this->hasOne('App\Employer');
}

public function jobseeker(){
  return $this->hasOne('App\Jobseeker','user_id');
}
}

这是我所做的编辑,以便&#39;状态&#39;可以添加到用户表中,因为它没有默认的laravel身份验证。

我将registerController的Create函数编辑到了

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'status' => $data['status'],
        'password' => bcrypt($data['password']),
    ]);
}

这是我的用户迁移表

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

希望这有帮助

1 个答案:

答案 0 :(得分:0)

Laravel默认添加时间戳。查看&#34;迁移&#34;中的create_users_table.php文件。夹。我假设你使用了

  

php artisan make:auth

命令,所以你的文件应该是这样的:

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();
    });
}

        $table->timestamps();

行正在添加&#34; created_at&#34;和&#34; updated_at&#34;字段。