Skip to main content

Home Screen: List of Events

Premium
12 min read

It's time to display Events for our users on their homepage:

This page will have two lists of Events:

  • Attending Events - this will be a list of Events the user pressed Attend on
  • Upcoming Events - this will be a list of Events that will happen within 30 days

Let's do this:


Adding Application Homepage

Let's start by creating a new Dashboard Livewire component:

php artisan make:livewire Dashboard

Then, we can modify the Dashboard class:

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 21 min)

You also get:

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

Already a member? Login here

Comments & Discussion

LL
Leonel López ✓ Link copied!

Regarding the two parameters filter and perPage in this getEvents method, the response returns null.

public function getEvents($filter, $perPage = 3) { return Http::asJson() ->withToken(session('token')) ->acceptJson() ->get(config('services.api.url') . '/events', [ 'filter' => $filter, 'per_page' => $perPage, ]) ->json(); }

It is not receiving any data. However, when I tested the API in Postman without filter or per_page parameters, it returned all the events successfully.

M
Modestas ✓ Link copied!

This could be due to a few reasons. But to know for sure - I need to understand:

  • Does this happen on mobile application?
  • Are you testing it in the browser or the phone?
  • If you are on the phone - are you using ngrok tu access your API?
LL
Leonel López ✓ Link copied!

I had to apply this practical solution because most APIs (such as those using Laravel API Resources or Laravel pagination) return a similar structure.

With this approach:

It extracts only the array inside "data".

If, for some reason, "data" does not exist, it returns an empty array [] instead of throwing an error.

This is safe and straightforward when you only need the list of events and not the pagination information.

private function getEvents(string $filter, int $perPage = 3): array { $response = Http::asJson() ->withToken(session('token')) ->acceptJson() ->get(config('services.api.url').'/events', [ 'filter' => $filter, 'per_page' => $perPage, ]);

return $response->json('data', []); // 👈 returns only the list
}

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.