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

MislavNovalic's avatar

EfforException - Attempt to read property "artwork_url" on null

Hi everyone. In the "Build Livewire App with me" series I am on the episode about styling form and displaying data(Part 1), at the end of the episode I encounter this error above in the title, error is on the:

  <x-button primary type="submit" class="w-full">Create Listening Party</x-button>

and here is my dashboard.blade.php file :

use Livewire\Volt\Component;
use App\Models\ListeningParty;
use Livewire\Attibutes\Validate;
use App\Models\Episode;
use App\Jobs\ProcessPodcastUrl;

new class extends Component {
    public string $name = '';

    public $startTime;

    string $mediaUrl = '';

    protected function rules()
    {
        return [
            'name' => 'required|string|max:255',
            'startTime' => 'required',
            'mediaUrl' => 'required|url',
        ];
    }

    public function createListeningParty()
    {
        $this->validate();

        $episode = Episode::create([
            'media_url' => $this->mediaUrl,
        ]);

        $listeningParty = ListeningParty::create([
            'episode_id' => $episode->id,
            'name' => $this->name,
            'start_time' => $this->startTime,
        ]);

        ProcessPodcastUrl::dispatch($this->mediaUrl, $listeningParty, $episode);

        return redirect()->route('parties.show', $listeningParty);
        }

public function with()
{
    return [
        'listeningParties' => ListeningParty::where('is_active', true)->orderBy('start_time', 'asc')->with('episode.podcast')->get(),
    ];
}
}; 
    <div class="my-20">
        @if ($listeningParties->isEmpty())
            <div>No audio parties started yet.</div>
        @else
            @foreach ($listeningParties as $listeningParty)
                <div wire:key="{{ $listeningParty->id }}">
                    <x-avatar src="{{ $listeningParty->episode->podcast->artwork_url }}" size="xl"
                        rounded="full" />
                    <p>{{ $listeningParty->name }}</p>
                    <p>{{ $listeningParty->episode->title }}</p>
                    <p>{{ $listeningParty->episode->podcast->title }}</p>
                    <p>{{ $listeningParty->start_time }}</p>
            @endforeach
        @endif    
    </div>
</div>
0 likes
3 replies
LaryAI's avatar
Level 58

The error you're encountering, "Attempt to read property 'artwork_url' on null," suggests that the episode or podcast relationship is returning null for at least one of your ListeningParty records. This typically happens when the expected related data is not present in the database.

Here's how you can troubleshoot and fix this issue:

  1. Check Relationships: Ensure that the episode and podcast relationships are correctly defined in your ListeningParty and Episode models. For example:

    // In ListeningParty model
    public function episode()
    {
        return $this->belongsTo(Episode::class);
    }
    
    // In Episode model
    public function podcast()
    {
        return $this->belongsTo(Podcast::class);
    }
    
  2. Database Integrity: Verify that all ListeningParty records have a corresponding Episode and that all Episode records have a corresponding Podcast. You can do this by checking your database records.

  3. Conditional Rendering: Modify your Blade template to handle cases where episode or podcast might be null. This will prevent the error from occurring and allow you to display a fallback message or image.

    @foreach ($listeningParties as $listeningParty)
        <div wire:key="{{ $listeningParty->id }}">
            @if ($listeningParty->episode && $listeningParty->episode->podcast)
                <x-avatar src="{{ $listeningParty->episode->podcast->artwork_url }}" size="xl" rounded="full" />
                <p>{{ $listeningParty->name }}</p>
                <p>{{ $listeningParty->episode->title }}</p>
                <p>{{ $listeningParty->episode->podcast->title }}</p>
                <p>{{ $listeningParty->start_time }}</p>
            @else
                <p>Episode or Podcast information is missing.</p>
            @endif
        </div>
    @endforeach
    
  4. Debugging: If the issue persists, you can add some debugging code to log or dump the listeningParties data to see which records are missing the relationships.

    dd($listeningParties);
    

By following these steps, you should be able to identify and resolve the issue causing the error.

tykus's avatar
tykus
Best Answer
Level 104

If there is no podcast associated with the Episode, then $listeningParty->episode->podcast->artwork_url will fail like this. You can mitigate for this with appropriate use of the null-safe operator ?->:

$listeningParty->episode?->podcast?->artwork_url

Of course, if you have taken steps elsewhere in the app to ensure the Episode belongs to a Podcast (and you have defined the podcast relationship on the Episode model), then there should never be an Episode without an associated Podcast

1 like

Please or to participate in this conversation.