Use Session in Laravel Framework

  1. Download and install the latest version of Composer from https://getcomposer.org/download/

  2. Open Windows Terminal in Visual Studio Code install Laravel Installer with command as below:

    composer global require laravel/installer
  3. Create new folder named LearnLaravelWithRealApps. Use Visual Studio Code open to LearnLaravelWithRealApps foler. In this folder, create new project named LearnLaravelWithRealApps with command as below:

  4. Run LearnLaravelWithRealApps project with command as below:

    php artisan serve
  5. Open LearnLaravelWithRealApps project with url as below:

Create new PHP file named Product.php in app/Models folder as below:

<?php

namespace App\Models;

class Product
{
    var $id;
    var $name;
    var $price;

    function __construct($id, $name, $price)
    {
        $this->id = $id;
        $this->name = $name;
        $this->price = $price;
    }
}

Create new PHP file named DemoController.php in app/Http/Controllers folder as below:

<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class DemoController extends Controller
{
	public function index(Request $request)
	{
		$request->session()->put('id', 123);
		$request->session()->put('username', 'acc1');

		$product = new Product('p01', 'name 1', 4.5);
		$request->session()->put('product', $product);

		$products = array(
			new Product('p01', 'name 1', 4.5),
			new Product('p02', 'name 2', 7.8),
			new Product('p03', 'name 3', 9.4)
		);
		$request->session()->put('products', $products);

		// Remove Session
		$request->session()->forget('id');

		return redirect('/demo/index2');
	}

	public function index2(Request $request)
	{
		if ($request->session()->has('id')) {
			echo 'id: ' . $request->session()->get('id');
		}

		if ($request->session()->has('username')) {
			echo '<br>username: ' . $request->session()->get('username');
		}

		if ($request->session()->has('product')) {
			$product = $request->session()->get('product');
			echo '<br>id: ' . $product->id;
			echo '<br>name: ' . $product->name;
			echo '<br>price: ' . $product->price;
		}

		if ($request->session()->has('products')) {
			$products = $request->session()->get('products');
			foreach ($products as $product) {
				echo '<br>id: ' . $product->id;
				echo '<br>name: ' . $product->name;
				echo '<br>price: ' . $product->price;
				echo '<br>------------------------';
			}
		}

		return view('demo/index');
	}
}

Create new folder named demo in resources/views folder. In this folder, create new Blade file named index.blade.php as below:

<html>

<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Laravel</title>
</head>

<body>

	<h3>Index</h3>

</body>

</html>

Open web.php file in routes folder, add new routes as below:

<?php
				
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\DemoController;

Route::group([], function () {
    Route::get('/', [DemoController::class, 'index']);
    Route::get('/demo', [DemoController::class, 'index']);
    Route::get('/demo/index', [DemoController::class, 'index']);
    Route::get('/demo/index2', [DemoController::class, 'index2']);
});

Access index action in Demo controller with url http://localhost:8000/demo/index to create new sessions and redirect to index2 action in Demo controller with url http://localhost:8000/demo/index2

Output