Multiple Apis With ForwardsCalls Trait - Laravel

Let's start quickly. I found an advanced way to call the api via ForwardsCalls and I want to share it with you.
First you create an Api.php page

<?php

namespace App\Http\Apis;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Traits\ForwardsCalls;

abstract class Api
{
    use ForwardsCalls;

    protected PendingRequest $http;

    public function __construct()
    {
        $this->http = $this->initialize();
    }

    public function __call($method, $params)
    {
        return $this->forwardCallTo($this->http, $method, $params);
    }

    public abstract function initialize(): PendingRequest;
}

Create a Laratips Api.php page to take from the previous page

<?php

namespace App\Http\Apis;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;

class LaratipsApi extends Api
{
    public function initialize(): PendingRequest
    {
        return Http::acceptJson()->baseUrl('http://laratips.test/api');
    }
}

So we come to the summons

<?php

namespace App\Http\Controllers;

use App\Http\Apis\LaratipsApi;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class DashboardController extends Controller
{
    public function index(Request $request)
    {
        return (new LaratipsApi)->get('users');
    }
}

It is also possible to call fakeapi and take a certain part of it I create a FakeStoreApi.php page

<?php

namespace App\Http\Apis;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;

class FakeStoreApi extends Api
{
    public function initialize(): PendingRequest
    {
        return Http::acceptJson()->baseUrl('https://fakestoreapi.com');
    }    
}

So we come to the summons

<?php

namespace App\Http\Controllers;

use App\Http\Apis\FakeStoreApi;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class DashboardController extends Controller
{
    public function index(Request $request)
    {
        return (new FakeStoreApi)->get('products', [
            'limit' => 2
        ]);
    }
}

I hope you enjoyed the code.

24