0

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);    
}
krlzlx
  • 5,752
  • 14
  • 47
  • 55
Dina Shaldoum
  • 89
  • 1
  • 4
  • 14
  • Can you better describe your problem? What was that you expected to see? And what did you see instead? – Koala Yeung Feb 15 '17 at 06:19
  • I'm trying to use the product variable at my footer (shared for each views ) . If i used this query : $products = ProductsTranslation::join('products', 'products.id', '=', 'products_translations.product_id') ->get(); I'm getting the all products at both of the language i have been used . I need to use where condition where('language',$lang) to only get the language for the current lang , but the $lang variable back with null, even after using the config \Config::get('languages') ; – Dina Shaldoum Feb 15 '17 at 06:38
  • Do you have `languages` in your config? – Koala Yeung Feb 15 '17 at 06:43
  • I'm not sure , it's working fine for the all website , i have using this topic for that https://mydnic.be/post/laravel-5-and-his-fcking-non-persistent-app-setlocale but the issue with sharing it at AppServiceprovider class – Dina Shaldoum Feb 15 '17 at 06:55
  • I understand what's the problem, the reason is that the variable called after the session is started or something , so how can i share my menu variable with the right way? – Dina Shaldoum Feb 15 '17 at 12:49

1 Answers1

0

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);
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64