Getting locale language at provider class in Laravel

匆匆过客 提交于 2019-12-12 01:29:42

问题


I'm new at Laravel and trying to make the variable of content at the header shared with all views, but the issue is with getting the language which backing with me with null value at the provider (AppServiceProvider) class.

Here's my code :

public function boot( )
{
    // $language=App::setLocale($locale);
    $locale = App::getLocale();
    \Session::put('language', 'en');
    \Config::get('app.locale');
    \Config::get('languages') ;
    \Session::get('languages', 'en');
    $lang = Session::get ('locale');   

    $products = ProductsTranslation::join('products', 'products.id', '=', 'products_translations.product_id')->where('language',$lang) ->get();                          

    $postId   = Post::get();
    view()->share('products', $products,'language',' \Session::get("language", $locale )','postId',$postId);    
}

回答1:


There are a few issues with the snippet:

  • The share() method only takes two arguments instead of repeated key, value pairings
  • The value intended for language is the result of Session::get("language", $locale), but what's actually put is the string ' \Session::get("language", $locale )'.

Based on that, you'd need to rewrite as following

view()->share('products', $products);
view()->share('language', Session::get('language', $locale));
view()->share('postId', $postId);


来源:https://stackoverflow.com/questions/42241782/getting-locale-language-at-provider-class-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!