Answer the question
In order to leave comments, you need to log in
How to catch session data in Laravel controller?
On the site I write functionality for working with several regions.
View:
@if(session()->has('city'))
{{ session('city') }}
@else
{{session(['city' => 'Base City'])}} //если пользователь впервые зашел на сайт, ставит базовый город
{{ session('city') }}
@endif
public function set(Request $request)
{
$city = $request->get('city'); //забирает данные установленные во вьюшке
session()->put('city', $city); //ставит обновленные данные в сессию
if (session()->has('city')) {
return redirect()->back();
}
echo 'Some kind of bug with the sessions of the cities';
}
$region = session('city');
$region = session()->get('city');
Answer the question
In order to leave comments, you need to log in
Why are you writing logic in views?
I would bring such general logic to the middleware level
public function handle($request, Closure $next)
{
$request->session()->get('city', 'default');
/* логика с переменной $city */
view()->share(['city' => $city]);
return $next($request);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question