5

I would like to make the menu dinamically, so instead showing "FILMS OF THE MONTH" it shows "FILMS OF DECEMBER" (see the image); being December the current month that update itself each month

My problem is that the menu does not belongs to an specific route/controller, therefore I can not pass the variable like I do with other routes. Example:

    $month = Carbon::now()->format('F');
    return view('partials._nav')
            ->withMonth($month);

I could "solve" that problem passing the month to ALL controllers/routes, but I am repeating many times the same code.

My questions are

1- Is there any way in Laravel to pass a variable to a kind of rootstate (like in Angular) so, I keep the variable available in All routes of my application?

2- Is there any way that I could return a variable to more than one view? So I just pass the variable to all views possible like:

return view('films.all', 'films.show', 'films.index', 'actors.show', 'actors.index')
                ->withMonth($month);

Any idea would be appreciated

The navigation is outside the scope of views UPDATE

The menu is outside the views scope

Finally I have the solution:

public function boot()
{
    View::composer('*', function ($view) {
        $month = Carbon::now()->format('F');

        $view->withMonth($month);
    });
}
3
  • 1
    I know this is not your expected answer but.. sublimetext.com :) Commented Dec 19, 2016 at 13:54
  • @everytimeicob or He can buy PHPStorm Licence for versioning control, remote FTP, SFTP, Terminal access in development envrionment and a full development support (autocomplete etc....) Commented Dec 19, 2016 at 13:57
  • @Disfigure yes, I agree so MMB Commented Dec 19, 2016 at 14:17

3 Answers 3

5

You can use the share() method to share the data to multiple view. Place the following code in your boot() method of App/Providers/AppServiceProvider:

public function boot()
{
    view()->share('key', 'value');
} 

Docs

Sign up to request clarification or add additional context in comments.

1 Comment

I try to understand what does it mean, but I have not clue how to use this in my solution. What is key?, what is value? I need to pass a variable $month that dynamically changes each month (january, february..) into all views.
2

You can use the AppServiceProvider, you pass a variable through the layout that you extends.

Your code will be:

public function boot()
{
    View::composer('*', function ($view) {
        $var = 'value';

        $view->with('var', $var);
    });

    View::composer('*', function ($view) {
        $month = Carbon::now()->format('F');

        $view->withMonth($month);
    });
}

And you need to use the following class: use Illuminate\Support\Facades\View;

Hope this works!

6 Comments

I used this solution , changing the line $var = 'value" with this -> "$var = Carbon::now()->format('F');" Since I want the value to be dynamic. I added use Carbon\Carbon at the top but the solution give me an error message: "FatalErrorException in AppServiceProvider.php line 26: Class 'App\Providers\View' not found"
It did not work. I get the error: Undefined variable: month (View: C:\Users\rra\Documents\Personal\Kino\resources\views\partials_nav.blade.php). I think the problem is that I am trying to pass the variable $month into the layout, outside all the views. See the image, I updated the question
@RafaelMunoz I added the code, this will pas to all views in your view folder the variable. Now it will also pass to the views which extends the layout.
It finally worked, but I had to make some changes. See the update in the question
@FarshidRezaei return view(‘admin.dashboard’, compact(‘varname’)); remind to not use the $ in the compact method.
|
0

Another way is to use Laravel Session. You can save variable value in session and get it anywhere.

 //To Save value in Session
   $month = 'January';
   Session::put('currentMonth', $month);

 //To Get value from Session
   if((Session::has('currentMonth'))) {
       $month_var = Session::get('currentMonth');
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.