Skip to main content

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

Read more here

Facility Structure: Show Property Details

Premium
10 min read

Now imagine the user clicking on the property to choose the apartment. Inside the property view, we need to show the full list of available apartments, with their facilities:

Property show facilities

The Facility model will be a new thing in this lesson.


Goals of This Lesson

  • Build Facility DB/relationship structure
  • Show facilities in the new Show Property endpoint

By the end of this lesson, we will have this list of facilities showing in Postman:

Property show facilities


Facilities: DB Structure

An obvious question: what are those facilities, and what are the different options and categories for them? There could be dozens or even hundreds of various facilities.

I've found the answer to this question while browsing the mobile version of Booking.com, which showed the apartment facilities in a well-structured way:...

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 Al Qahtani ✓ Link copied!

Are you sure this about this line?

$response->assertJsonPath('0.apartments.0.facilities', NULL);

PK
Povilas Korop ✓ Link copied!

From what I remember in my testing yes it was NULL. Can you explain your thought why this is wrong?

AA
Ali Al Qahtani ✓ Link copied!

Sorry, You right, the test comes success

V
vpinti ✓ Link copied!

I have carefully read the guide you published on property listings and I have a question regarding the invoke method in the PropertyController. In the section where data is loaded using the load method, I noticed that the load method is called twice for the apartments.facilities relationship. I'm wondering why it is necessary to reload the relationship at this stage, as it seems that the relationship is already included in the $property object in the first load('apartments.facilities') call. Could you please explain why a second load of the same relationship is being performed?

I read in the guide that it mentions overriding the values of apartments, but I'm not clear on how this is connected to the second load of the relationship. Could you kindly provide further details on this point?**

PK
Povilas Korop ✓ Link copied!

You're probably right, we don't need to load it the second time. I will update the tutorial, great catch!

N
nazzalra ✓ Link copied!

When setup migration for facilities table, the reason why u re using references('id')->on('facility_categories') instead of using constrained because the table name is not categories. But we can use constrained function and put the table name as first argument like constrained('facility_categories', 'id'), can't we?

N
nazzalra ✓ Link copied!

Sir, firstly thankyou for this tutorial i'm learning a lot. I followed this guide until i found an error on PropertyShowTest in part :

    $response->assertJsonPath('apartments.0.facilities.0.name', $facility->name);

the error message is "property show loads property correctly. Failed asserting that null is identical to 'Some Facility" i try to debug using dd($response->json()). i notice the facilities field is not exist Although i already load the apartments.facilities relationship on the PropertyController. But when i change $this->whenLoaded('facilities') to $this->facilities (on ApartmentSearchResource), the facilities field exists and the test passes. I spend hours trying to solve this but still no solution yet. Could u help me to solve this? Or what im missing? Thanks

PK
Povilas Korop ✓ Link copied!

To be honest, I don't remember now as the course has been created a few months ago, and I can't "blindly" debug your situation. Could you please compare your code to the official repository and maybe see what you're missing? Or maybe the error is in the official repository, too? Then let me know. Thanks!

MV
Mauricio Villatoro ✓ Link copied!

Hi, the problem is when you filter by adults and children in the controller, you only load facilities relationship when you don't send query params.

In the guide, the code is this:

class PropertyController extends Controller
{
 public function __invoke(Property $property, Request $request)
 {
 $property->load('apartments.facilities');
 
 if ($request->adults && $request->children) {
 $property->load(['apartments' => function ($query) use ($request) {
 $query->where('capacity_adults', '>=', $request->adults)
 ->where('capacity_children', '>=', $request->children)
 ->orderBy('capacity_adults')
 ->orderBy('capacity_children');
 }]);
 }
 
 return new PropertySearchResource($property);
 }
}

But if you add the facilities relationship into query params, all test pass.

class PropertyController extends Controller
{
 public function __invoke(Property $property, Request $request)
 {
 $property->load('apartments.facilities');
 
        if ($request->adults && $request->children) {
            $property->load(['apartments' => function ($query) use ($request) {
                $query->where('capacity_adults', '>=', $request->adults)
                    ->where('capacity_children', '>=', $request->children)
                    ->orderBy('capacity_adults')
                    ->orderBy('capacity_children');
            }, 'apartments.facilities']);
        }
 
 return new PropertySearchResource($property);
 }
}
N
nazzalra ✓ Link copied!

Wow thank you that solve the issue, much appreciated