Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Lazy Loading Components

Premium
2 min read

Sometimes you have a component where it will have a lot of data to fetch from the DB and then do some calculations before showing stats or charts. Or you have a big blog with millions of comments, and it takes some time to load a post because of those comments. Let's see how we can load some components after loading the whole page.


In the earlier lesson, we added comments. Let's add a new ShowComments Livewire component.

use App\Models\Post;
use Livewire\Component;
use Illuminate\Support\Collection;
use Illuminate\Contracts\View\View;
 
class ShowComments extends Component
{
public Collection $comments;
 
public function mount(Post $post): void
{
$this->comments = $post->comments()->get();
}
 
public function render(): View
{
return view('livewire.show-comments');
}
}
<div class="space-y-1">
@foreach($comments as $comment)
<p>{{ $comment->body }}</p>
@endforeach
</div>

It's just a simple component where we get comments for the post and show the body of the comment.

Typically we would call this component like...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

AA
Ali Awwad ✓ Link copied!

this is way cleaner than v2 wire:init then creat method then load collection inside render method

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.