Skip to main content
Dub

Laravel

Learn how to integrate Dub with Laravel.
2 min read

1. Prerequisites#

To follow this guide, you will need to:

2. Install and initialize the Dub PHP SDK#

Install#

To install the Dub PHP SDK, run the following command:

Configuration#

In your .env file, add your Dub API key:

In your config/services.php file, add the following:

Initialize#

You can now create an instance of the Dub class and pass in your API key:

index.php

Service Container (Optional)#

If you want to be able to inject the Dub class via the service container, add this to the register method of your AppServiceProvider.php:

index.php

You can then inject the authenticated Dub instance throughout your application:

index.php

Let's create a short link using the Dub Laravel SDK.

index.php

Optionally, you can also pass an externalId field to associate the link with a unique identifier in your own system.

index.php

This will let you easily update the link or retrieve analytics for it later on using the externalId instead of the Dub linkId.

The Dub Laravel SDK provides a method to upsert a link – where an existing link is updated if it exists, or a new link is created if it doesn't.

index.php

This way, you won't have to worry about checking if the link already exists when you're creating it.

To update an existing link using the Dub Laravel SDK, you can either use the link's linkId or externalId.

index.php

You can also retrieve analytics for a link using the Dub Laravel SDK.

index.php

7. Examples#