Reputation: 87
I want to get Auth ID from user who has logged in and then use the Auth ID to store on other table
User_detail Controller
this is my store function
$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');
and this is function Login
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 !!! ' );
}
}
}
this is the routes files
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);
}
and I get error like this
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
Upvotes: 0
Views: 1630
Reputation: 211
should be Auth::id()
or Auth::user()->id
but seems like your Auth::user()
is returning a null.make sure you sessions, routes are set up properly.
use Auth::attempt()
to login user
Upvotes: 0
Reputation: 3562
You don't need to use $data->fill(Auth::user());
as you have only single user_id
field need to set.
Also you can get the current logged in user's id using. \Auth::user()->id
So your code would be as follow:
$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');
Note: Make sure you have included auth
middleware with your route.
Like:
Route::get('profile', ['middleware' => 'auth', function() {
// Only authenticated users may enter...
}]);
And you have followed the authuntication process carefully.
https://laravel.com/docs/5.2/authentication
Edited:
Your loging should be changed as:
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 !!! ' );
}
Upvotes: 0
Reputation: 2355
If your reverse one-to-one relationship in the User Model looks like this:
public function detail()
{
return $this->hasOne(ModelUser::class);
}
And you are sure a user is logged in, you could simply do this
$data = Auth::user()->detail()->save($request->all());
return redirect()->route('surveylist');
Laravel's ORM takes care of the rest.
Upvotes: 0