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));
});
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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;
}
}
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));
});
Please or to participate in this conversation.