Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

Using session

LoginController.php

public function login(Request $request)
{

    $this->validate($request, [
      'username' => 'required|min:6|max:15',
      'password' => 'required|min:8|max:16',
      ]);

    if(auth()->attempt(['username' => request('username'), 'password' => request('password')]))
    {
        $id = User::find($id);

        $request->session()->put('id', $id);

        return redirect('/seller');
    }

    session()->flash('flash', 'username atau password anda salah atau belum terdaftar.  Mohon mendaftarkan diri di link di bawah ini jika belum.');

    return redirect('/login');

}

Check this Login Controller. I actually want to make the program remember the id of who is login. Can you do that? Is that using what's called session?

Check this part of the codes:

if(auth()->attempt(['username' => request('username'), 'password' => request('password')]))
    {
        $id = User::find($id);

        $request->session()->put('id', $id);

        return redirect('/seller');
    }

Is that correct? Since you are only login using username and password. and I need to know the $id (primary key) and remember it.

I am going to retrieve the id to retrieve other data after login based on the user_id or id.

0 likes
5 replies
Vilfago's avatar
Vilfago
Best Answer
Level 20

If you use the standard auth process in laravel, you can retrieve the current user with Auth::user()(or Auth::id(), if you only want the id).

So yes it's possible with session, but it's almost native. https://laravel.com/docs/5.6/authentication

davy_yg's avatar
Level 27

Does this syntax works:

 User::create([
        'no_ktp' => request('no_ktp'),
        'foto_ktp' => $original_name_ktp,
        'foto_profile' => $original_name_profile
    ])->where('id', auth::id());

It seems like the where part does not work.

SQLSTATE[HY000]: General error: 1364 Field 'email' doesn't have a default value (SQL: insert into users (no_ktp, foto_ktp, foto_profile, updated_at, created_at) values (12345678, photo.jpg, photo.jpg, 2018-04-22 02:12:58, 2018-04-22 02:12:58))

It looks like trying to create a new row instead of adding to the existing one.

Cronix's avatar

If you already know the id, it means the record is already created and you should be using update instead of create. Create is for new records.

davy_yg's avatar
Level 27

Thanks it works!

One more thing: how to set the validation length = 16 digit

This does not gives the correct message:

'no_ktp' => 'required|numeric|min:16|max:16',

Please or to participate in this conversation.