Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dr24's avatar
Level 2

Problem with view composer in AppServiceProvider

I have user with user profile data stored in database and I am able to read that data on user profile page. What I want is to be able to fetch that data (variable is $profile) on any view in my application so I made view composer in boot method in AppServiceProvider.php but now I have problem where it says that variable $name is undefined. I copied the code from my controller method show where I passed variable $name in function show, like this public function show($name), but now when I passed the code in boot method I don't have that variable defined. How can solve that and make some workaround? Any help is appreciated. Here is my code.

AppServiceProvider.php

<?php

namespace App\Providers;

use App\User;
use App\UserProfile;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Schema\Builder; // Import Builder where defaultStringLength method is defined

class AppServiceProvider extends ServiceProvider
{
    /**
    * Register any application services.
    *
    * @return void
    */
    public function register()
    {
        
    }

    /**
    * Bootstrap any application services.
    *
    * @return void
    */
    

    public function boot()
    {
        view()->composer('*', function ($view) {
            if(!Auth::check()) {
                abort(401);
            }
            $viewer = User::find(Auth::user()->id);
    
            $owner = User::where('name', $name)->first();
    
            // Check if user with given username exists
            if($viewer->id !== $owner->id) {
                return abort(404);
            }
    
            if(!$profileId = User::getIdFromName($name)){
                return abort(404);
            }
    
            $profile = UserProfile::profileDetails($profileId, $viewer->id);
    
            View::share('profile_details', $profile);
    
            $view->with('profile_details', $profile);  
        
        }); 

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

}

UserProfileController.php

<?php

namespace App\Http\Controllers;

use App\User;
use App\UserProfile;
use Illuminate\Support\Facades\Auth;

class UserProfileController extends Controller
{
    /**
    * Display the specified resource.
    *
    * @param \App\Property $property
    * @return \Illuminate\Http\Response
    */
    public function show($name)
    {
        $viewer = User::find(Auth::user()->id);

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

        // Check if user with given username exists
        if($viewer->id !== $owner->id)
        {
            return abort(404);
        }

        if(!$profileId = User::getIdFromName($name)){
            return abort(404);
        }

        $profile = UserProfile::profileDetails($profileId, $viewer->id);

        //dd($profile);

        return view('profile.show', compact('profile'));
    }
}

UserProfile.php

<?php

namespace App;

use App\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;

class UserProfile extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public static function profileData($profileId, $viewerId)
    {
        $user = User::find($viewerId);
        $profile = DB::table('users as u')
            ->select(DB::raw("
                data.*,
                u.id, u.gender_id, u.name, u.email, u.age, u.premium
            "))
            ->leftJoin('user_profiles as data', 'data.user_id', '=', 'u.id')
            ->where('u.id', $profileId)
            ->first();

        return $profile;
    }

    public static function profileDetails($profileId, $viewerId)
    {
        $profile = static::profileData($profileId, $viewerId);
        
        if ($profile) {
            if ($viewerId != $profileId) {
                # Viewer is displaying another user's profile.
                $myProfile = false;
                } else {
                    $myProfile = true;
                }
            }

            # Populate profile data array
            $profile = [
                'viewerId' => $viewerId,
                'profile_id' => $profileId,
                'myProfile' => $myProfile,
                'status' => Value::fieldLabel('status', $profile->status),
                'first_name' => $profile->first_name,
            
            ];

        return $profile;
    }

}
0 likes
7 replies
Sti3bas's avatar
Sti3bas
Best Answer
Level 53

So you want to access the profile of authenticated user in all of your views?

If so, use this:

view()->composer('*', function ($view) {
   $view->with('profile_details', UserProfile::profileDetails(Auth::user()->id, Auth::user()->id));  
}); 
dr24's avatar
Level 2

Yes. And I changed the code in boot method like you wrote. And now when I try to display something in blade like this {{$profile_details->first_name}} I get Trying to get property 'first_name' of non-object . Can you help?

Sti3bas's avatar

@gacho that's because your profileDetails method returns an array, not an object. Change it to {{ $profile_details['first_name'] }}

2 likes
dr24's avatar
Level 2

Ah yes. Just now I realized that. Now it works perfectly. Thank you very much!

1 like
dr24's avatar
Level 2

@sti3bas I have now just one small problem. When I logout I get error Trying to get property 'id' of non-object in AppServiceProvider. It is because the user is not logged in therefore there is no auth user id. How can I resolve that?

Sti3bas's avatar
view()->composer('*', function ($view) {
   $view->with('profile_details', $userId = Auth::id() ? UserProfile::profileDetails($userId, $userId) : []);  
}); 
2 likes

Please or to participate in this conversation.