Home > Backend Development > PHP Tutorial > Using Fluent to Work With HTTP Client Responses in Laravel

Using Fluent to Work With HTTP Client Responses in Laravel

Robert Michael Kim
Release: 2025-03-06 01:01:10
Original
486 people have browsed it

Using Fluent to Work With HTTP Client Responses in Laravel

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:

<!-- Syntax highlighted by torchlight.dev -->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
", ...]
Copy after login

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

<!-- Syntax highlighted by torchlight.dev -->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)
}
*/
Copy after login

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:

<!-- Syntax highlighted by torchlight.dev -->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');
Copy after login

#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.

The above is the detailed content of Using Fluent to Work With HTTP Client Responses in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template