我想从已登录的用户获取身份验证ID,然后使用身份验证ID存储在其他表上
用户详细信息控制器
这是我的商店功能
$data = new ModelUser();
$user= new user();
$data->fill(Auth::user());
$data->id_user = Auth::get('id');
$data->jenis_kelamin = $request->jenis_kelamin;
$data->no_tlp = $request->no_tlp;
$data->jurusan = $request->jurusan;
$data->wilayah = $request->wilayah;
$data->save();
return redirect()->route('surveylist');
这是登录功能
public function LoginPost(Request $request)
{
$email = $request->email;
$password = $request->password;
$data = user::where('email',$email)->first();
if($data) //check email apakah ada atau tidak
{
if(Hash::check($password,$data->password))
{
Session::put('id',$data->id);
Session::put('full_name',$data->full_name);
Session::put('email',$data->email);
Session::put('login',TRUE);
return redirect('userdt');
}
else
{
return redirect('index')->with('alert','Password atau Email yang anda masukan salah !!! ' );
}
}
}
这是路线文件
Route::get('/index','UserController@show')->name('surevey.index');
Route::get('/logout','UserController@Logout')->name('user.logout');
Route::post('/registerpost','UserController@RegisterPost')->name('user.register');
Route::post('/loginpost','UserController@LoginPost')->name('user.login');
//reward routes
Route::get('/reward','RewardController@index')->name('reward.list');
//profile
Route::put('/editprofile/edit/{id}','UserController@edit')->name('profile.edit');
Route::post('/editprofile/update','UserController@update')->name('profile.update');
Route::get('/userdt',['middleware'=>'auth','uses'=>'UserController@userdetail'])->name('userdt.show');
Route::post('/userdt/store','UserController@store')->name('userdt.store');
//Survei
Route::get('/createsurvey','SurveyController@show')->name('survey.create');
Route::get('/surveylist','SurveyController@index')->name('survey.list');
Auth::routes();
ModelUser
protected $fillable = [
'id_user',
'jenis_kelamin',
'no_tlp',
'jurusan',
'wilayah'
];
protected $table ='user_detail';
public function user()
{
return $this->belongsTo(user::class);
}
我得到这样的错误
传递给Illuminate \ Database \ Eloquent \ Model :: fill()的参数1必须 属于数组类型,给定null,在 第110行的E:\ Laravel \ surevey \ app \ Http \ Controllers \ UserController.php
答案 0 :(得分:3)
您不需要使用$data->fill(Auth::user());
,因为只需设置一个user_id
字段即可。
还可以使用获取当前登录用户的ID。 \Auth::user()->id
所以您的代码如下:
$data = new ModelUser();
$data->id_user = \Auth::user()->id;
$data->jenis_kelamin = $request->jenis_kelamin;
$data->no_tlp = $request->no_tlp;
$data->jurusan = $request->jurusan;
$data->wilayah = $request->wilayah;
$data->save();
return redirect()->route('surveylist');
注意:请确保您的路由中包含auth
中间件。
赞:
Route::get('profile', ['middleware' => 'auth', function() {
// Only authenticated users may enter...
}]);
您已经仔细遵循了认证过程。
https://laravel.com/docs/5.2/authentication
已编辑:
您的日志记录应更改为:
public function LoginPost(Request $request)
{
$email = $request->email;
$password = $request->password;
if (Auth::attempt(['email' => $email, 'password' => $password])) {
// Authentication passed...
return redirect()->intended('userdt');
}
return redirect('index')->with('alert','Password atau Email yang anda masukan salah !!! ' );
}
答案 1 :(得分:0)
应为Auth::id()
或Auth::user()->id
,但似乎您的Auth::user()
返回空值。请确保您进行会话,路由设置正确。
使用Auth::attempt()
登录用户
答案 2 :(得分:0)
如果您在用户模型中的一对一反向关系如下所示:
public function detail()
{
return $this->hasOne(ModelUser::class);
}
您确定用户已登录,只需执行此操作
$data = Auth::user()->detail()->save($request->all());
return redirect()->route('surveylist');
Laravel的ORM负责其余的工作。