'How to Use Macro in Laravel Http::Pool?
I want to add a macro to Http::loop to define base url. I am using Laravel 9, so far I followed these steps:
1 - Add Macro to app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Http;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Http::macro('adequateshop', function () {
return Http::baseUrl('http://restapi.adequateshop.com');
});
}
2 - Trying to access the macro using Http::loop
$responses = Http::pool(fn (Pool $pool) => [
$pool->adequateshop()->get('/api/Tourist?page=1'),
$pool->adequateshop()->get('/api/Tourist?page=2'),
$pool->adequateshop()->get('/api/Tourist?page=3'),
$pool->adequateshop()->get('/api/Tourist?page=4'),
]);
I get this error:
BadMethodCallException: Method Illuminate\Http\Client\PendingRequest::adequateshop does not exist.
I tried to access the macro (without Http::loop) and it worked:
$response = Http::adequateshop()->get('/api/Tourist?page=1');
Question 1: Does anyone knows if it is possible to access macros under Http::loop()
Question 2: Can I have multiple macros like this:
$responses = Http::pool(fn (Pool $pool) => [
$pool->adequateshop()->get('/api/Tourist?page=1'),
$pool->github()->get('/api/endpoint'),
$pool->google()->get('/api/endpoint'),
$pool->stackoverflow()->get('/api/endpoint'),
]);
Thanks for your help and time!
Solution 1:[1]
Thanks @lagbox, for your suggestion. This code is working:
app/Providers/AppServiceProvider.php
public function boot()
{
PendingRequest::macro('adequateshop', function () {
return PendingRequest::baseUrl('http://restapi.adequateshop.com');
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | osroflo |
