1 month ago

Passing single variable across multiple controllers?

Posted 1 month ago by sjking85

I'm sorry if it sounds like such a stupid question, I'm new to Laravel and I'm really enjoying the learning so far. I hope you can help with a query.

After much messing about, I have managed to display a simple count of the number of users in the User database.

I would like to do this on two views with different controllers, namely UserController and HomeController.

I have done so by simply having on my HomeController:

public function index()
        $users = User::count();

        return view('dashboard', compact('users'));

and in my blade file I have:

<span class="h2 font-weight-bold mb-0">{{ $users }}</span>

This shows up as expected.

The issue is I would like it also to be displayed on the view using the UserController, do I need to edit my index to show this too?

I have added the following to my pre-existing code:

public function index(User $model)
        $users = User::count();

        return view('users.index', ['users' => $model->paginate(15)], compact('users'));

...but it doesn't show the count.

Is there a way I can define it globally to then call on all pages? Any help for this rookie is appreciated.

Please sign in or create an account to participate in this conversation.