我跟隨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()
爲什麼沒有正常工作?
您是否檢查過數據庫以查看是否進行了更改?我不認爲'$ user-> save()'返回任何東西,這就是爲什麼你不把它放到那個'if'塊中的原因。 – mopo922 2015-01-20 20:47:29
That'Whoops,貌似出了點問題。'消息是因爲你的debug設置爲false。打開你的'app/config/app.php'文件並將'debug'鍵設置爲true,然後重試。這一次,你會得到一個詳細說明這個異常的頁面,你可以從那裏調試,或者用你遇到的實際錯誤更新這個問題。 – patricus 2015-01-20 21:05:35