How to echo session variable in laravel?

Member

by jasen , in category: PHP Frameworks , 4 months ago

How to echo session variable in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , 21 days ago

@jasen 

To echo a session variable in Laravel, you can simply use the session() helper function to retrieve the value of the session variable and then echo it in your view file. Here's an example:


In your controller:

1
2
3
4
5
6
public function showSessionVariable()
{
    session()->put('name', 'John Doe');
    
    return view('session_variable');
}


In your view file (session_variable.blade.php):

1
2
3
4
5
6
7
8
9
<!DOCTYPE html>
<html>
<head>
    <title>Session Variable Example</title>
</head>
<body>
    <h1>Hello, {{ session('name') }}</h1>
</body>
</html>


In this example, we've set the session variable name to the value John Doe in the controller and then echoed it in the view file using the session('name') function. This will output "Hello, John Doe" on the page.