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

hidayat3676's avatar

how to use session in laravel

in my scenario i am developing a blogging application my flow is that user will first either registered or login to start posting. i want when a user registered it self it shall be redirected to their page where they can see their post and perform crud on their post, and if user click on home button there they can see posts from all user including their own. how can i track the user in this multiple pages.

0 likes
12 replies
Sys32's avatar

The session is carried over until you logout of the laravel app.

You can read more about the default auth and sessions here: https://laravel.com/docs/5.6/authentication

Each user has its own database entry, and you can change the page they see, after logging in, to redirect to their latest post etc.

hidayat3676's avatar
my user controller method where user is  regitered

 public function store(Request $request)
    {
        if (isset($request['register'])){
         $name     = $request->input('name');
         $email    = $request->input('email');
         $password = Hash::make($request->input('password'));
         $users = User::all();
         foreach ($users as $user)
         {
              if($user->email == $email){
                                 echo "<h2>Email '".$user->email.  "'already exits!</h2><br/>";
                                 echo "If you already have an account click here<a href='/login'>Log In</a> ";
                                 return view('user.createuser');

              }

         }

            DB::insert('insert into users (name,email,password) values(?,?,?)',[$name,$email,$password]);

            return redirect('My Posts');
        }


2. my route after registration
Route::get('My Posts','PostController@userposts');

3. my post controller function
 public function userPosts(){
        $posts = post::all();
            return view('post.userposts',compact('posts'));

    }

now how can i get posts of this user in the userposts view 
Sys32's avatar

You shouldn't create your own signup form, as Laravel have it built in already.

Your signup redirect is also broken, should use the route below.

Your route is wrong aswell

Route::get('My Posts','PostController@userposts');

Should be

Route::get('/account/posts','PostController@userposts');

Then going to example.com/account/posts will view the userposts() function.

rin4ik's avatar
 public function store(Request $request)
    {
        if (isset($request['register'])){
         $name     = $request->input('name');
         $email    = $request->input('email');
         $password = Hash::make($request->input('password'));
         $users = User::all();
         foreach ($users as $user)
         {
              if($user->email == $email){
                                 echo "<h2>Email '".$user->email.  "'already exits!</h2><br/>";
                                 echo "If you already have an account click here<a href='/login'>Log In</a> ";
                                 return view('user.createuser');

              }

         }

            DB::insert('insert into users (name,email,password) values(?,?,?)',[$name,$email,$password]);

            return redirect('/posts/user');
        }

Route::get('/posts/user','PostController@userPosts');
 public function userPosts(){
        $posts = Post::where('user_id', auth()->id);
            return view('post.userposts',compact('posts'));

    }
hidayat3676's avatar
Route::get('/account/posts','PostController@userposts');

where  is this account come from to route

rin4ik's avatar

@hidayat3676 Route::get('My Posts','PostController@userposts'); this will not work . define another route with slash. route can't be with space

hidayat3676's avatar

bro this route is working and user is redirected to view but my problem is that how i can get this newly created user in that view

Sys32's avatar

@hidayat3676 that was just an example. You obviously need to use whatever route url, that corresponds to your application.

But the route you have set, does not work as is. You need to define a place, where each individual user can find their posts. Like /posts/user or /account/user etc.

hidayat3676's avatar

@ rin4ik

 public function userPosts(){
        $posts = Post::where('user_id', auth()->id);
            return view('post.userposts',compact('posts'));

    }

how i can get the user id here
rin4ik's avatar

@hidayat3676 auth()->user()->id or just auth()->id()

 public function userPosts(){
        $posts = Post::where('user_id', auth()->user()->id);
            return view('post.userposts',compact('posts'));

    }
hidayat3676's avatar

then this id would be id of that user that has just create an account?

rin4ik's avatar

yes this id is authenticated user. you can use auth()->user() to get all user informations

Please or to participate in this conversation.