在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) } */
>,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中文網其他相關文章!