2017-06-20 128 views
-1

我一直工作在一個應用程序的某個時候現在..突然我得到這個奇怪的錯誤。顯示的數據與我在數據庫中的數據不同。 這就是我在我的數據庫」Laravel顯示不同的數據從數據庫數據

id - 1 
name - Jon Doe 
email - [email protected] 
status - jobseeker 
created_at - 2017-06-20 21:02:47 
updated_at - 2017-06-20 21:02:47 

但是,當我顯示的數據,這是我得到

{"id":1,"name":"Jon Doe","email":"[email protected]","status":"employer","created_at":"2017-06-20 21:02:47","updated_at":"2017-06-20 21:02:47","employer":null} 

而我使用的刀片「{{驗證::用戶()}}「以登錄後顯示用戶信息

我認爲它與vardump數據時的」屬性「和」原始「有關。

我不知道在哪裏的變化正在發生。我正在使用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'); 
} 
} 

這是編輯我這樣做的「狀態」可以被添加到用戶表因爲它沒有使用默認的laravel認證。

我編輯的registerController的創建功能,這

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

我知道你認爲我們在#1,是魔術師告訴你什麼是錯的不看任何代碼的,但說實話,我們只是凡人人類。請在您從數據庫獲取數據以及如何處理數據的地方發佈相關代碼。至於最後兩個字段,Laravel默認情況下將其添加 – Silencer310

+0

抱歉..我加入再次編輯更多信息 –

+0

..顯示的數據是不是在數據庫中 –

回答

0

Laravel默認情況下,添加時間戳。查看「migrations」文件夾中的create_users_table.php文件。我假設你使用的

PHP工匠製作: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(); 

線增加「created_at」和「updated_at」字段。

+0

請我加入了更多的信息 –

+0

你能描述你的問題有點多? –

+0

讓你困惑,爲什麼狀態是在輸出「員工」在你的數據庫,但「求職者」? –