I use this middleware to check/set the language in session for every request:
<?php
namespace App\Http\Middleware;
use App;
use Auth;
use Config;
use Session;
use Closure;
class SetLocale
{
public function handle($request, Closure $next)
{
// If the session doesn't have already a locale
if (!Session::has('locale')) {
// Set the logged in user language
if (Auth::check() && Auth::user()->lang->code) {
Session::put('locale', Auth::user()->lang->code);
} else {
// Else get the http header language and set it
$requestLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if (App\Lang::where('code', $requestLanguage)->exists()) {
Session::put('locale', $requestLanguage);
} else {
// If none of the above worked use the app deafult language
Session::put('locale', Config::get('app.locale'));
}
}
}
// Set the output locale as app locale
App::setLocale(Session::get('locale'));
return $next($request);
}
}
Hope this helps you.