Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Model binding with external API service

Hello,

I'm using the pipedrive API in a project and I'd like to use model binding.

Is it possible to write a custom model binding resolver that would send an HTTP request to the API to retrieve a model ?

I have read the documentation about explicit route model binding, but I don't understand how to do given that my model can't be an Eloquent model.

Thanks for your help.

V

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

Yes, you can use custom route model binding in Laravel to bind a route parameter to a model retrieved from an external API like Pipedrive. Since your model isn't an Eloquent model, you'll need to create a custom binding resolver. Here's how you can do it:

  1. Create a Custom Model Class:

    First, create a class that represents the model you want to bind. This class will hold the data you retrieve from the API.

    class PipedriveModel
    {
        public $id;
        public $name;
        // Add other properties as needed
    
        public function __construct($data)
        {
            $this->id = $data['id'];
            $this->name = $data['name'];
            // Initialize other properties
        }
    }
    
  2. Create a Service to Fetch Data from the API:

    Create a service class that will handle the HTTP request to the Pipedrive API.

    use Illuminate\Support\Facades\Http;
    
    class PipedriveService
    {
        public function find($id)
        {
            $response = Http::get("https://api.pipedrive.com/v1/your-endpoint/{$id}", [
                'api_token' => 'your_api_token',
            ]);
    
            if ($response->successful()) {
                return new PipedriveModel($response->json()['data']);
            }
    
            return null;
        }
    }
    
  3. Register the Custom Binding in a Service Provider:

    In your AppServiceProvider or a dedicated service provider, register the custom binding.

    use Illuminate\Support\Facades\Route;
    use App\Services\PipedriveService;
    use App\Models\PipedriveModel;
    
    public function boot()
    {
        Route::bind('pipedriveModel', function ($value) {
            $service = new PipedriveService();
            return $service->find($value);
        });
    }
    
  4. Define Your Route:

    Use the custom binding in your route definition.

    use App\Models\PipedriveModel;
    
    Route::get('/pipedrive/{pipedriveModel}', function (PipedriveModel $pipedriveModel) {
        return response()->json($pipedriveModel);
    });
    

With this setup, when you access a route like /pipedrive/123, Laravel will automatically resolve the 123 parameter using your custom binding logic, fetch the data from the Pipedrive API, and inject an instance of PipedriveModel into your route's closure or controller method.

1 like

Please or to participate in this conversation.