Search code examples
laravel-livewire

Completly skip rendering of component


How can I set the component to only show if there are videos?

<?php

namespace App\Http\Livewire;

use App\Models\Video;
use Livewire\Component;

class VideosBrowse extends Component
{    
    // Computed Property
    public function getVideosProperty()
    {
        return Video::latest()->paginate(10);
    }

    public function output($errors = null)
    {
        if (!$this->videos || $this->videos->isEmpty()) {
            $this->skipRender();
        }

        return parent::output($errors);
    }

    public function render()
    {
        return view('livewire.videos.browse');
    }
}

View:

<div id="videos-browse">
    @if ($this->videos && $this->videos->isNotEmpty())
        Videos
    @endif
</div>

Solution

  • You have to run skipRender() on render

    public function render() {
    
         if (!$this->videos || $this->videos->isEmpty()) {
             $this->skipRender();
         }
    
        return view('livewire.videos.browse');
    }