1

I am trying to make a global variable in AppServiceProvider.php that I will need throught my whole application meaning in all blade files. This variable is $profile which gets the profile data from user and displays them in blades. I made it so when I am on my profile it shows authenticated user which is me and it is fine (in url is like this profile/Authuser), that Authuser is username from database. Problem is when I go to some other profile then I get error undefined username (in url profile/Someuser). I need help on to get that username in AppServiceProvider.php. Problem is in that $username in service provider. I don't know how to pass it in there globally. Any help is appreciated. Here is my code.

AppServiceProvider.php

public function boot()
{ 

    $profileId = $this->getIdFromUsername($username);  // Here is problem, I don't know how to get that username

    view()->composer('*', function ($view) {
        $view->with('profile', Auth::id() ? UserProfile::profileDetails($profileId, Auth::user()->id) : []);  
    });

    Builder::defaultStringLength(191); // Update defaultStringLength
}

public function getIdFromUsername($username)
{
    if ($user = User::where('username', $username)->first()) {
        return $user->id;  
    }

    return abort(404);  
}

web.php

Route::get('profile/{profile}', 'UserProfileController@showProfile')->name('profile.show');
mrmar
  • 1,407
  • 3
  • 11
  • 26

1 Answers1

1

I believe you are over complicating yourself.

If I understand your app. A user has a Profile correct?

Go to your User Model and create a relation between User and Profile

public function userProfile()
    {
        return $this->hasOne('App\UserProfile');
    }

With that, the profile will follow the user, and you don't need to be passing it around. If you want the Profile for the current User.

Auth::user()->userProfile();

If you want the profile of another user then

$owner = User::where('username', $username)->first();
$owner->userProfile();

Basically you can have access to the profile of your logged in user, or any other user easily by just finding the user you want.

Now, if you really wish to have a Model in every view, you are placing it in the wrong place. You see, Service Providers are intended to tie things up, not to get data. What you are probably thinking about is a View Composer that you do tie in with a Service Provider, but the actual data comes from the Composer itself. You can learn more about View Composer in the Docs. https://laravel.com/docs/7.x/views#view-composers

View Composers are just one way of doing it, a quick google search brought up this question which offers 3 additional alternatives to the view composer. How to pass data to all views in Laravel 5?

Hope that helps.

Arturo Alvarado
  • 498
  • 1
  • 5
  • 10
  • I have everything like this. My only problem is where and how do I get $username variable from $owner = User::where('username', $username)->first(); – mrmar Apr 13 '20 at 08:33
  • 1
    From my experience, when I was new in Laravel, I often thought about Global variables that would be everywhere, such as a cart. I quickly learned that most of the time, I had access to the data though out a relationship with the Auth user. That's why I pointed you towards rethinking your approach. – Arturo Alvarado Apr 14 '20 at 12:07
  • 1
    However, to answer your question directly. You can't get $username from the request, because that is not what Service Providers are meant for. See both boot() and register() methods of any service provider are called before request is parsed and request parameters are known, that's why you can't access them. https://laravel.com/docs/7.x/providers https://stackoverflow.com/questions/38764406/get-url-parameter-from-service-provider-in-laravel – Arturo Alvarado Apr 14 '20 at 12:11
  • 1
    yeah, you are right, I found the solution that works for me. thank you – mrmar Apr 14 '20 at 12:24
  • I expanded the answer to point you to options on how to pass a Model to all your views. – Arturo Alvarado Apr 14 '20 at 12:34