如何从用户 table 获取授权 ID 并将其存储到数据库中的其他 table

How To Get Auth ID form user table and grab it for store to other table on database

我想从已登录的用户那里获取 Auth ID,然后使用 Auth ID 存储在其他 table

User_detail 控制器

这是我的商店功能

$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();

模型用户

protected $fillable = [
    'id_user',
    'jenis_kelamin',
    'no_tlp',
    'jurusan',
    'wilayah'
];

protected $table ='user_detail';

public function user()
{
    return $this->belongsTo(user::class);
}

我得到这样的错误

Argument 1 passed to Illuminate\Database\Eloquent\Model::fill() must be of the type array, null given, called in E:\Laravel\surevey\app\Http\Controllers\UserController.php on line 110

应该是 Auth::id()Auth::user()->id 但看起来你的 Auth::user() 正在返回一个 null.make 确定你的会话,路由设置正确。

使用Auth::attempt()登录用户

您不需要使用 $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 !!! ' );
}

如果您在用户模型中的反向 one-to-one 关系如下所示:

 public function detail()
{
    return $this->hasOne(ModelUser::class);
}

并且您确定用户已登录,您可以简单地执行此操作

$data = Auth::user()->detail()->save($request->all());
return redirect()->route('surveylist');

Laravel 的 ORM 负责其余的工作。