3

I'm very new to Laravel. As a blind programmer, I hope to utilize it to help me better improve my programming skills, speed, and accuracy. But I'm learning by hand as some videos are very visual. At this time, I'm trying to share a variable across all views and getting an undefined variable error.

I'm doing it by placing the following in the App\Http\Controllers\controller.php file:

public function __construct() {
$time = Carbon::now()->subMinute(5);
$users_online = User::where('last_active', '>', $time)
->where('last_active', '!=', '0000-00-00    00:00:00');
View::share('users_online', $users_online->count());
}

I'm calling it in various ways, none of which seem to work. Any help would be greatly appreciated.

For reference, I'm getting a variable not defined error.

LorienDarenya
  • 439
  • 1
  • 4
  • 8
  • 1
    possible duplicate of http://stackoverflow.com/questions/28608527/how-to-pass-data-to-all-views-in-laravel-5 . If this doesn't solve your problem, give more info. Also, try to dd users_online to check. – Bharat Geleda Nov 24 '15 at 17:09
  • Thank you; I'd accept your answer if my screenreader could see how to. – LorienDarenya Nov 24 '15 at 22:08

1 Answers1

1

You should put all your global View variables in AppServiceProvider inside boot() method.

For example:

public function boot()
{
    $time = Carbon::now()->subMinute(5);
    $users_online = User::where('last_active', '>', $time)
        ->where('last_active', '!=', '0000-00-00    00:00:00');
    View::share('users_online', $users_online->count());
    ...
}
izupet
  • 1,529
  • 5
  • 20
  • 42