阅读(2717) (6)

Laravel 8 抛出异常

2021-07-05 10:24:27 更新

如果你希望请求在发生客户端或服务端错误时抛出 Illuminate\Http\Client\RequestException 异常,你可以在请求实例上调用 throw 方法:

$response = Http::post(...);

// 在客户端或服务端错误发生时抛出异常
$response->throw();

return $response['user']['id']; 

Illuminate\Http\Client\RequestException 实例拥有一个 $response 公共属性,该属性允许你检查返回的响应。

return Http::post(...)->throw()->json();