jeudi 26 novembre 2015

User object to every controller if authenticated

The 'problem' I'm facing now is that with every controller that is used when the user is authenticated I have to request $user = Auth::user();. An example:

public function store(Request $request)
{
    $user = Auth::user();

    $building = Building::findOrFail($request->building);
    $user->buildings()->save($building);

    return response(['status' => 'OK']);
}

As you can imagine I need to request the user object many more times, even in this one controller. How can I improve this? I have thought about declaring a user variable in the Controller parent class, or using Middleware. I read about dependency injection but I don't fully understand it and I can't figure out how to implement it well.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire