2015-01-20 5 views
1

나는 this Laravel login/register tutorial on YouTube을 따르고 있으며 문제가 발생했습니다.Laravel : 사용자 개체를 데이터베이스에 저장할 수 없습니다.

$user 개체의 데이터를 데이터베이스에 삽입 할 수없는 것 같습니다. $user->save() 메서드에 도달 할 때까지 내가 지금까지 가지고있는 모든 것이 완벽하게 작동합니다.

다음은 내 AccountController.php입니다. 프로세스를 디버그하고 디버그하기 위해 print_r을 사용하고 있습니다. 첫 번째 print_r은 내 페이지에 인쇄되지만 두 번째 페이지는 인쇄되지 않습니다. Laravel이 경고를 중지하고 암호문 Whoops, looks like something went wrong.을 출력합니다. 이러한 모든 요소가 실제로 내 사용자 테이블입니다

use Illuminate\Auth\UserTrait; 
use Illuminate\Auth\UserInterface; 
use Illuminate\Auth\Reminders\RemindableTrait; 
use Illuminate\Auth\Reminders\RemindableInterface; 

class User extends Eloquent implements UserInterface, RemindableInterface { 

    protected $fillable = array('active', 'name', 'email', 'password', 'password_temp', 'code', 'salt', 'created_at', 'updated_at', 'pref_weight', 'pref_units', 'pref_time', 'pref_ener'); 

    use UserTrait, 
     RemindableTrait; 

    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'users'; 

    /** 
    * The attributes excluded from the model's JSON form. 
    * 
    * @var array 
    */ 
    protected $hidden = array('password', 'remember_token'); 

} 

:

class AccountController extends BaseController { 

    public function getCreate() 
    { 
     return View::make('account.create'); 
    } 

    public function postCreate() 
    { 
     $validator = Validator::make(Input::all(), array(
        'email' => 'required|max:64|min:3|email|unique:users', 
        'name' => 'required|max:64|min:3', 
        'password' => 'required|max:64|min:6' 
     )); 

     if ($validator->fails()) 
     { 
      // Return to form page with proper error messages 
      return Redirect::route('account-create') 
          ->withErrors($validator) 
          ->withInput(); 
     } 
     else 
     { 
      // Create an acount 
      $email = Input::get('email'); 
      $name = Input::get('name'); 
      $password = Input::get('password'); 

      // Activation code 
      $code = str_random(64); 
      $user = User::create(array(
         'active' => 0, 
         'email' => $email, 
         'username' => $name, 
         'password' => Hash::make($password), 
         'code' => $code 
      )); 

      if ($user) 
      { 
       // Send the activation link 
       Mail::send('emails.auth.activate', array(
        'link' => URL::route('account-activate', $code), 
        'name' => $name 
         ), function($message) use($user) { 
        $message 
          ->to($user->email, $user->username) 
          ->subject('Jasl | Activate your new account'); 
       }); 

       return Redirect::route('home') 
           ->with('success', 'One more step! You\'ll get an email from us soon. Please follow the activation link to activate your account.'); 
      } 
     } 
    } 

    public function getActivate($code) 
    { 
     // Find user whose code corresponds to the one we've previously sent through email 
     $user = User::where('code', '=', $code)->where('active', '=', 0); 

     if ($user->count()) 
     { 
      $user = $user->first(); 

      $user->active = 1; 
      $user->code = ''; 

      echo '<pre>', print_r($user), '<pre>'; 
      if ($user->save()) 
      { 
       echo '-----------------------'; 
       echo '<pre>', print_r($user), '<pre>'; 
      } 
     } 
    } 
} 

나는 조금 봤 내가 내 User 클래스의 $fillable 배열을 만들 것을 발견, 그래서 그것을 한 적이 있다.

이렇게해도 문제가 해결되지 않았습니다.

무엇이 누락 되었습니까? $user->save()이 제대로 작동하지 않는 이유는 무엇입니까?

+0

변경 사항이 적용되었는지 확인하기 위해 데이터베이스를 확인 했습니까? 나는'$ user-> save()'가 아무것도 리턴하지 않는다고 생각한다. 그래서 당신은'if' 블록에 넣지 않는다. – mopo922

+2

그게 틀린 것 같아요. 메시지는 당신이 디버그를 false로 설정했기 때문입니다. 'app/config/app.php' 파일을 열고'debug' 키를 true로 설정 한 다음 다시 시도하십시오. 이번에는 예외에 대해 자세히 설명하는 페이지가 표시되며 거기에서 디버그하거나 문제가있는 실제 오류로이 질문을 업데이트 할 수 있습니다. – patricus

답변

0

알겠습니다.

내 문제는 id 대신 사용자 이름이 user_id 인 사용자 테이블의 id 열을 만들었습니다. 분명히 Laravel은 전혀 좋아하지 않습니다.

SQLSTATE [42S22] : 발견되지 칼럼 : 1054 알 수없는 열 'ID'에서 (SQL '절'업데이트를 usersactive = 1을 설정 오류가있는

C:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php 

: 디버거에 저를 지적 , 당신이 id 열을 사용자 정의하지 말아야 몰랐다

code = updated_at = 2015년 1월 20일 id가 null 21시 28분 14초). 이름을 변경하면 문제가 완전히 해결되고 데이터베이스가 올바르게 업데이트됩니다.

유용한 디버깅 팁에 대해 @patricus에게 감사 드리며,이 오류로 인해이 오류를 추적 할 수있었습니다.

관련 문제