Working with third-party APIs can be frustrating; we get JSON responses which in PHP will be represented as a plain old array - and we send data as arrays too. What if I told you that it doesn’t take much effort to build something that will add more context and improve your work with third-party APIs?

declare(strict_types=1); namespace App\Services\MedicalTrust\Resources; use Illuminate\Http\Client\Response; class DentalResource { public function __construct( private readonly MedicalTrustService $service, ) {} public function list(string $identifier): Response { return $this->service->get( request: $this->service->buildRequestWithToken(), url: "/dental/{$identifier}/records", ); } public function addRecord(string $identifier, array $data = []): Response { return $this->service->post( request: $this->service->buildRequestWithToken(), url: "/dental/{$identifier}/records", payload: $data, ); }}

Let’s have a look at how an object like Crowns might look: app/Services/MedicalTrust/DataObjects/Crowns.php declare(strict_types=1); namespace App\Services\MedicalTrust\DataObjects; use Illuminate\Support\Collection; class Crowns { public function __construct( public Collection $treatments, ) {} public function toArray(): array { return $this->treatments->map(fn (DentalTreatment $treatment) => $treatment->toArray(), )->toArray(); }}

Firstly let’s create the data object for the request:app/Services/MedicalTrust/Requests/NewDentalTreatment.php declare(strict_types=1); namespace App\Services\MedicalTrust\Requests; class NewDentalTreatment { public function __construct( public readonly string $type, public readonly string $material, public readonly string $location, public readonly Carbon $implemented, ) {} public function toArray(): array { return [ 'type' => $this->type, 'material' => $this->material, 'location' => $this->location, 'implemented' => Carbon::now()->toDateString(), ]; }}
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners