在Laravel 11.2.0中,
fluent()
,我们得到了一个
<!-- 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 ", ...]
>另一个整洁的功能是将JSON数据转换为特定类型。以此示例为例,我们可以将字符串日期转换为Carbon
>实例:
<!-- 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) } */Fluent还支持其他有用的类型,例如
>,boolean
,enum
的数组等等。我的最爱之一是使用熟悉的方法和enum
和only
检索特定数据:except
<!-- 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');
以上是使用Fluent与Laravel中的HTTP客户响应一起工作的详细内容。更多信息请关注PHP中文网其他相关文章!