Wednesday, March 1, 2017

Laravel dependecy injection Auth::

I have a laravel project with a CalendarService and I inject that service into my controller. In the constructer I do something like this:

CalendarService.php

/** @var Collection|Timelog[] */
private $timelogs;

public function __construct()
{
    $this->currentRoute = URL::to( '/' ) . "/home";
    $this->timelogs     = Auth::user()->timelogs()->get();
    $this->currentDay   = 0;
}

HomeController.php

/** @var CalendarService */
protected $calenderService;

public function __construct
(
    CalendarService $calendarService
)
{
    $this->calenderService = $calendarService;
}

And I get this error

Call to a member function timelogs() on null

About this line of code:

Auth::user()->timelogs()->get();

I have used the use Illuminate\Support\Facades\Auth; in my service

Does anyone know what is going on here any help is much appreciated!

Thanks in advance!




via Frank Lucas

Advertisement