How to echo session variable in laravel?

Member

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

How to echo session variable in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , 6 months 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.

Related Threads:

How to echo array elements in laravel?
How to echo output of artisan::call() in laravel?
How to echo the name of a 'module' in my laravel app?
How to create 2 variable session in codeigniter?
How to separate user session from admin session in laravel?
How to share session from laravel to wordpress?