Skip to main content

Show Facilities in Apartment Details

Premium
4 min read

Now, let's move to the detailed view of the specific Apartment which would show facilities grouped by category.

This will be a bit shorter lesson compared to others.


Goals of This Lesson

  • New endpoint to Show Apartment
  • Grouping the facility data
  • PHPUnit test for it

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

Apartment show facilities Postman


New Endpoint: Show Apartment

Again, from the previous lesson, here's how the apartment detail facilities screen looks on mobile:...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 33 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

AA
Ali Al Qahtani ✓ Link copied!

I think there is this not right syntax in ApartmentShowTest This line:

$response->assertJsonFragment($expectedFacilityArray, 'facility_categories');

This is right syntax one is:

$response->assertJsonFragment(['facility_categories' => $expectedFacilityArray]);

see more in Laravel documentation: https://laravel.com/docs/10.x/http-tests#assert-json-fragment

PK
Povilas Korop ✓ Link copied!

Hmmm, could it be that BOTH can work? Cause the test passed successfully.

AA
Ali Al Qahtani ✓ Link copied!

Maybe, But the function assertJsonFragment accepts one argument (array $data)

NS
Ngozi Stephen Onyemauche ✓ Link copied!

Hi, i am getting this error when i run my test assertCount(): Argument #2 ($haystack) must be of type Countable|iterable, null given, called in C:\laragon\www\BookingApp\vendor\lar avel\framework\src\Illuminate\Testing\AssertableJsonString.php on line 74

$response = $this->getJson('/api/apartments/'.$apartment->id); 61▕ $response->assertStatus(200); 62▕ $response->assertJsonPath('name', $apartment->name); ➜ 63▕ $response->assertJsonCount(2, 'facility_categories'); 64▕ 65▕ $expectedFacilityArray = [ 66▕ $firstCategory->name => [ 67▕ $firstFacility->name,

Pls what should i do i don't understand this error message
PK
Povilas Korop ✓ Link copied!

Probably your "facility_categories" relationship is returned empty or null, so it's impossible to compare to number 2.

NS
Ngozi Stephen Onyemauche ✓ Link copied!

okey i understand now

V
vpinti ✓ Link copied!

be sure to use the resource ApartmentDetailsResource in the controller

NS
Ngozi Stephen Onyemauche ✓ Link copied!
$response = $this->getJson('/api/apartments/'.$apartment->id);
$response->assertStatus(200);
$response->assertJsonPath('name', $apartment->name);
$response->assertJsonCount(2, 'facility_categories');
 
$expectedFacilityArray = [
$firstCategory->name => [
$firstFacility->name,
$secondFacility->name
],
$secondCategory->name => [
$thirdFacility->name
]
];
 
$response->assertJsonFragment($expectedFacilityArray, 'facility_categories');
```

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.