Hi All,
I have a simple middleware where i want to set the country inside the cookie. The cookie is setting and i can view. But its not working for the first time. when the page loads.
When i refresh the page at the second time. then it works. But first time not works.
What is the issue.
Any Help will be appreciated..
My code is
<?php
namespace App\Http\Middleware;
use Closure;
use Modules\Location\Entities\Country;
class CheckCountry
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if($request->hasCookie('country'))
{
return $next($request);
}
// dd('ddd');
$country = Country::where('service_supported', 1)->where('status', 1)->get();
if($country->count()==1){
$response = $next($request);
return $response->withCookie(cookie()->forever('country', $country));
}
}
}
And My middleware group is
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
// \App\Http\Middleware\SetLocale::class,
\App\Http\Middleware\CheckCountry::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];