Php Laravel:无法将用户对象保存到数据库
我在跟踪,我遇到了一个问题 似乎无法将$user对象中的数据插入数据库。 在达到$user->save方法之前,我所拥有的一切都非常好 下面是my AccountController.php。您会注意到,我正在使用print\r尝试调试该过程。第一次打印被打印到我的页面上,但第二次从未打印过:Laravel只是停下来,输出一声神秘的呜呜声,看起来好像出了什么问题。警告 这些实际上是我的users表中的所有元素 这并没有解决问题 我错过了什么?为什么$user->save不能正常工作?我知道了 我的问题是,我用自定义名称user_id而不是简单的id创建了users表的id列。显然,Laravel根本不喜欢这样。调试器向我指出: 错误如下: SQLSTATE[42S22]:未找到列:1054 where子句中的未知列“id”SQL:update users set active=1,code=,updated_at=2015-01-20 21:28:14,其中id为null 我不知道你不应该自定义id列。重命名它完全解决了问题,数据库现在可以正确更新Php Laravel:无法将用户对象保存到数据库,php,laravel,Php,Laravel,我在跟踪,我遇到了一个问题 似乎无法将$user对象中的数据插入数据库。 在达到$user->save方法之前,我所拥有的一切都非常好 下面是my AccountController.php。您会注意到,我正在使用print\r尝试调试该过程。第一次打印被打印到我的页面上,但第二次从未打印过:Laravel只是停下来,输出一声神秘的呜呜声,看起来好像出了什么问题。警告 这些实际上是我的users表中的所有元素 这并没有解决问题 我错过了什么?为什么$user->save不能正常工作?我知道了 我
感谢@patricus提供了有用的调试技巧,这使我能够跟踪此错误。您是否检查了数据库以查看是否进行了更改?我认为$user->save不会返回任何东西,这就是为什么你没有进入if块的原因。哎呀,看起来好像出了什么问题。消息是因为您已将调试设置为false。打开app/config/app.php文件并将调试键设置为true,然后重试。这一次,您将获得一个详细说明异常的页面,您可以从那里进行调试,也可以用您遇到的实际错误更新此问题。
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>';
}
}
}
}
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');
}
C:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php