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

lukeboy_2002's avatar

Profile Image

Hi,

I want to add a profile Image to a user. The image is a part of the user profile what is a different Model. The function profile Image is a section of the livewire profile show page.

I added the following to the show.blade.php

<livewire:profile-image :user="$user"/>

In my User model I have.

    public function profile(): HasOne
    {
        return $this->hasOne(Profile::class);
    }

In my Profile model I have:

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

I created a livewire component ProfileImage:

and the logic

How to connect the active user to this components. I get all kinds off errors.

Can someone help me.

0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To connect the active user to your Livewire component and handle profile image uploads, you need to ensure that the component receives the correct user data and that the logic is properly set up to handle the image upload. Here's a step-by-step solution:

  1. Pass the User to the Livewire Component:

    When you include the Livewire component in your Blade view, make sure you pass the authenticated user to it. You can do this by using Laravel's auth() helper.

    <livewire:profile-image :user="auth()->user()" />
    
  2. Modify the Livewire Component to Accept the User:

    Update your Livewire component to accept a User model instance. You can then use this user to access or create the associated profile.

  3. Ensure the Profile Relationship is Set Up:

    Make sure your User model has the correct relationship defined to access the profile:

    public function profile(): HasOne
    {
        return $this->hasOne(Profile::class);
    }
    
  4. Blade View Adjustments:

    Ensure your Blade view is correctly set up to display the Livewire component and handle any potential errors. You might want to add error handling for the image upload process.

  5. Testing:

    Test the component by uploading an image and ensuring it is saved to the correct media collection. Check for any errors in the console or network tab to debug issues.

By following these steps, you should be able to connect the active user to the Livewire component and handle profile image uploads without errors.

Snapey's avatar

make sure you use the correct name for mount()

Tell us the error

Merklin's avatar

If you are using Jetstream, there is an existing trait HasProfilePhoto.

Please or to participate in this conversation.