I'm trying to do dependency injection in Laravel to keep my controllers and models as slim as possible. The goal is to have repositories to handle the fetching of data attributed to certain models.
To this end I'm trying to follow the example from the documentation here and a popular Laravel boilerplate here
But I don't understand where the $user
is coming from.
So looking at the boilerplate we have two files:
The ProfileController
here
Excerpt below:
use App\Repositories\Frontend\Access\User\UserRepository;
/**
* Class ProfileController.
*/
class ProfileController extends Controller
{
/**
* @var UserRepository
*/
protected $user;
/**
* ProfileController constructor.
*
* @param UserRepository $user
*/
public function __construct(UserRepository $user)
{
$this->user = $user;
}
This looks a lot like the dependency injection mentioned in the docs, which is this:
class UserController extends Controller {
/**
* The user repository instance.
*/
protected $users;
/**
* Create a new controller instance.
*
* @param UserRepository $users
* @return void
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
My problem is I don't understand where the $user
is coming from.
In the UserRepository there is no $user defined as a parameter of the class itself. No where in the code is there any Auth::user()
so I'm confused as to where the user instance is coming from.
via Summer Developer