所以我使用的是laravel 4.2。我的问题是我无法回复重定向路由中的数据。我怎样才能以正确的方式访问它?我试着找到正确的答案,但它没有帮助我。
public function postLogin()
{
$timeIn = date('Y-m-d G:i:s');
$userLog = New UserLog;
$userLog->username = Input::get('username');
$userLog->time_in = $timeIn;
$userLog->save();
$users = $userLog;
return Redirect::route( 'account' )
->with( 'users', $users );
}
public function account(){
$data = Session::get('users');
var_dump($data );
echo $data->username;// this one throws an error
}
我可以看到它在var_dump($ data)中有值。
object(__PHP_Incomplete_Class)[92]
public '__PHP_Incomplete_Class_Name' => string 'UserLog' (length=7)
public 'timestamps' => boolean false
public 'table' => string 'userlogs' (length=8)
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
protected 'attributes' =>
array (size=3)
'username' => string 'user' (length=4)
'time_in' => string '2015-07-28 15:11:43' (length=19)
'id' => int 240
protected 'original' =>
array (size=3)
'username' => string 'user' (length=4)
'time_in' => string '2015-07-28 15:11:43' (length=19)
'id' => int 240
这些是我的路线文件
Route::get('/login', array(
'uses' => 'TimeController@login',
'as' => 'login')
);
Route::post('/postLogin', array(
'uses' => 'SessionsController@postLogin',
'as' => 'postLogin')
);
Route::get('/account', array(
'uses' => 'SessionsController@account',
'as' => 'account')
);
答案 0 :(得分:1)
我觉得我只是通过显示数据取得了巨大的成功,导致我得到错误的问题是__PHP_Incomplete_Class,你可以在我上面的var_dump($ data)上看到。要正确访问$ data,我需要在访问常规方法之前先将其反序列化。在这里,我只添加了一行代码,一切都运行良好。感谢这篇文章unserializing。
public function account(){
$data = Session::get('users');
$data = unserialize(serialize($data)); //added code to unserialize the __PHP_Incomplete_Class
var_dump($data);
echo $data->username;
}
在我的var_dump($ data)中,__PHP_Incomplete_Class现已消失
object(UserLog)[143]
public 'timestamps' => boolean false
protected 'table' => string 'userlogs' (length=8)
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
protected 'attributes' =>
array (size=3)
'username' => string 'user' (length=4)
'time_in' => string '2015-07-28 19:57:08' (length=19)
'id' => int 276
protected 'original' =>
array (size=3)
'username' => string 'user' (length=4)
'time_in' => string '2015-07-28 19:57:08' (length=19)
'id' => int 276