2015-01-20 111 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.警告。

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>'; 
      } 
     } 
    } 
} 

我GOOGLE了一下,发现我应该创造我User$fillable阵列,所以我做到了:

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'); 

} 

那些实际上所有的元素,我的用户表具有。

这并没有解决问题。

我错过了什么? $user->save()为什么没有正常工作?

+0

您是否检查过数据库以查看是否进行了更改?我不认为'$ user-> save()'返回任何东西,这就是为什么你不把它放到那个'if'块中的原因。 – mopo922 2015-01-20 20:47:29

+2

That'Whoops,貌似出了点问题。'消息是因为你的debug设置为false。打开你的'app/config/app.php'文件并将'debug'键设置为true,然后重试。这一次,你会得到一个详细说明这个异常的页面,你可以从那里调试,或者用你遇到的实际错误更新这个问题。 – patricus 2015-01-20 21:05:35

回答

0

我明白了。

我的问题是我用自定义名称user_id创建了我的用户表的id列,而不是简单的id。显然Laravel根本不喜欢这个。调试器向我指出:

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

与错误:

SQLSTATE [42S22]:柱未找到:1054未知列 'ID' 在 'where子句'(SQL:更新users设置active = 1 ,code =,= updated_at 2015年1月20日21点28分14秒,其中id为null)

我不知道,你不应该自定义id列。重命名完全解决了问题,数据库现在可以正确更新。

感谢@patricus提供了有用的调试技巧,这就是我能够跟踪这个错误的原因。