php - Laravel: simplest/right way to save a new step after login? -


i'm having first interaction core laravel code want careful not break anything.

in project, users correspond person records (via user->person_id), have get_person_from_user() function takes \auth::user() (conveniently accessible anywhere) , returns person object, can grab person record authenticated user controller , pass view.

the problem: there's piece of data person record i'd include in nav partial in default blade view (which gets extended bunch of different views), it's 1 case i'm not going through controller first. i'm unclear on how can make logged in user's person record available here. suggestions?

i think need add step after user logs in, save person record (globally? in session?) it's accessible. login stuff happens in authenticatesusers.php, , reading around sounds i'll want add override of postlogin authcontroller. tried copying function authenticatesusers.php authcontroller (not adding else yet), , authcontroller gives me new error when try log in:

reflectionexception in routedependencyresolvertrait.php line 81: class app\http\controllers\auth\request not exist 

any advice on way go accessing person object authenticated user, when don't have controller pass along?

you can setup correct relationship on user model person model.

public function person() {     return $this->belongsto(person::class); } 

then can do:

auth::user()->person; 

for having variable available particular view can use view composer. (you can create , register service provider , add register method.) potentially this:

view()->composer('someview', function ($view) {     if ($user = auth::user()) {         $somevar = $user->person->somevar;     } else {         $somevar = null; // or default     }     $view->with('somevar', $somevar); }); 

if view used in scenario doesn't have authed want check if auth::user() null before trying use relationship.

laravel docs - views - view composers


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -