Answer the question
In order to leave comments, you need to log in
How to properly set up a session in Laravel?
Good afternoon.
Please help me figure out how sessions are configured correctly in Laravel?
As an example, let's implement a page open counter. If you write a script in pure PHP in a view, then no questions asked - how would everything work:
@php
if (!isset($_SESSION['count2']))
{
$_SESSION['count2'] = 1;
}
else
{
++$_SESSION['count2'];
}
echo "Число открытия страницы: {$_SESSION['count2']}";
@endphp
public function countVisit(Request $request)
{
$request->session()->put('count', 1);
$count = $request->session()->get('count');
if(!isset($count)){
$count = 1;
}else{
++$count;
}
return view('form.test', ['count' => $count]);
}
Answer the question
In order to leave comments, you need to log in
$counter = $request->session()->get('counter', 1);
$request->session()->put('counter', ++$counter);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question