Laravel Tutorials

Using Fluent to Work With HTTP Client Responses in Laravel

Published
Using Fluent to Work With HTTP Client Responses in Laravel image

In Laravel 11.2.0, we got a fluent() helper to conveniently convert array data into a Fluent instance. Now, starting in Laravel 11.35, we have a convenient method for transforming an HTTP client response into a fluent instance:

use Illuminate\Support\Facades\Http;

$response = Http::get('https://jsonplaceholder.typicode.com/posts')->fluent();

$response->get('0.title'); // sunt aut facere...
$response->collect()->pluck('title'); // ["sunt aut facere...", "qui est esse
", ...]

Another neat feature is converting JSON data into specific types. Take this example where we can convert a string date into a Carbon instance:

use Illuminate\Support\Facades\Http;

$response = Http::get('https://api.chucknorris.io/jokes/random')->fluent();

$response->date('created_at');
$response->date('updated_at');

/*
Illuminate\Support\Carbon @1578231741 {#261 ▼ // routes/web.php:9
  date: 2020-01-05 13:42:21.455187 UTC (+00:00)
}
*/

Fluent also supports other helpful types like boolean, enum, array of enum, and more. One of my favorites is using familiar methods like only and except to retrieve specific data:

use Illuminate\Support\Facades\Http;

$response = Http::get('https://api.chucknorris.io/jokes/random')->fluent();

$response->except('categories'),
/*
array:6 [▼ // routes/web.php:9
  "created_at" => "2020-01-05 13:42:19.897976"
  "icon_url" => "https://api.chucknorris.io/img/avatar/chuck-norris.png"
  "id" => "KqoQdIJdSE2ezokPmHSvdw"
  "updated_at" => "2020-01-05 13:42:19.897976"
  "url" => "https://api.chucknorris.io/jokes/KqoQdIJdSE2ezokPmHSvdw"
  "value" => "One night Chuck Norris had Chili for dinner. The very next day the Big Bang happened."
]
*/

$response->only('id', 'url', 'value');

Learn More

I would recommend getting familiar with the Fluent class in the Laravel API docs. Fluent uses the InteractsWithData trait, which gives us a bunch of convenient methods for working with Fluent data.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article
Laranon: Reversible PII Anonymization for Laravel and LLMs image

Laranon: Reversible PII Anonymization for Laravel and LLMs

Read article
PHP 8.6 is released!  image

PHP 8.6 is released!

Read article
Laravel Warrant: Row-Level Permissions With Eloquent image

Laravel Warrant: Row-Level Permissions With Eloquent

Read article
JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43 image

JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43

Read article
Durable Workflow: Long-Running Workflows for Laravel image

Durable Workflow: Long-Running Workflows for Laravel

Read article