我正在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>';
}
}
}
}
我用谷歌搜索了一下,发现我应该在我的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()
不能正常工作?
我明白了。
我的问题是我使用自定义名称创建了用户表的id
列, user_id
,而不是简单地id
.显然拉拉维尔根本不喜欢这样。调试器指出我:
C:'xampp'htdocs'laravel'vendor'laravel'framework'src'Illuminate'Database'Connection.php
出现错误:
SQLSTATE[42S22]:找不到列:1054 "where 子句"中的未知列"id"(SQL:更新users
设置active
= 1,code
= ,updated_at
= 2015-01-20 21:28:14,其中id
为空)
我不知道您不应该自定义id
列。重命名它完全解决了这个问题,数据库现在可以正确更新。
感谢@patricus有用的调试提示,这就是允许我跟踪此错误的原因。