A package for PHP to interact with the GitHub Sponsors GraphQL API

Published on by

A package for PHP to interact with the GitHub Sponsors GraphQL API image

I'm very happy to say that Tom and I finally launched PHP GitHub Sponsors, a package for PHP to interact with the GitHub Sponsors GraphQL API.

Currently, PHP GitHub Sponsors allows you to do ACL based checks to see if one GitHub account is sponsoring another. While the package is framework agnostic so it can be used in any PHP application, it also comes with integration for the Laravel framework.

Here's an example how you'd use it. First, set up a personal access token in your .env file:

GH_SPONSORS_TOKEN=ghp_xxx

This access token should have the user:read and organization:read scopes.

Then, you can start using the package:

use GitHub\Sponsors\Facades\GitHubSponsors;
 
// Check if driesvints is being sponsored by nunomaduro...
GitHubSponsors::login('driesvints')->isSponsoredBy('nunomaduro');
 
// Check if the blade-ui-kit organization is sponsored by nunomaduro...
GitHubSponsors::login('nunomaduro')->isSponsoring('blade-ui-kit');
 
// Check if the authenticated user is sponsored by Gummibeer...
GitHubSponsors::viewer()->isSponsoredBy('Gummibeer');
 
// Check if the authenticated user is sponsoring driesvints...
GitHubSponsors::viewer()->isSponsoring('driesvints');

You can also retrieve the client from the IoC container and perform calls on it:

use GitHub\Sponsors\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
 
Route::get('/home', function (Request $request, Client $sponsors) {
$isSponsoring = $sponsors->viewer()->isSponsoredBy(
$request->get('github_username')
);
 
if ($isSponsoring) {
return redirect('/secret-homepage');
}
 
return view('welcome');
});

Sponsorable

Additionally, you can add the GitHub\Sponsors\Concerns\Sponsorable trait to an Eloquent model like the User model to make it sponsorable:

use GitHub\Sponsors\Concerns\Sponsorable;
use GitHub\Sponsors\Contracts\Sponsorable as SponsorableContract;
use Illuminate\Database\Eloquent\Model;
 
class User extends Model implements SponsorableContract
{
use Sponsorable;
}

Then, the user will use the value of its github column as its username. A user can also add their personal access token on the github_token column to let them check their private sponsorships.

Now you can use the model as follows:

$user = User::where('github', 'driesvints')->first();
 
// Check if driesvints is being sponsored by nunomaduro...
$user->isSponsoredBy('nunomaduro');
 
// Check if driesvints is sponsoring nunomaduro...
$user->isSponsoring('driesvints');

For more usage and details, check out the documentation.

Roadmap

Initially I wanted to provide much more features in the package. But because I only managed to work on the package sporadically over the past few months I felt that it wasn't coming along fast enough. That's why I decided to release it early with a minimum feature set and continue to work on it in the open.

We still have quite a bit of planned for the package. Here's some of the features on our roadmap:

Conclusion

This package couldn't have happened without some awesome people. I'd really like to thank Claudio Dekker for all his feedback and help while developing the package. I'd also like to thank Sarah Vessels for all her help with providing feedback on the GitHub GraphQL API. And lastly I want to thank Tom Witkowski for joining me as a co-maintainer and helping out with developing and improving the package.

I hope you'll like this one and if you do end up using the package definitely let me know on Twitter. Enjoy!

Dries Vints photo

Dries is a developer at Laravel where he maintains the open source projects. He's also the maintainer of Laravel.io, the Laravel community portal. And he's the creator of Blade UI Kit.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Asymmetric Property Visibility in PHP 8.4 image

Asymmetric Property Visibility in PHP 8.4

Read article
Access Laravel Pulse Data as a JSON API image

Access Laravel Pulse Data as a JSON API

Read article
Laravel Forge adds Statamic Integration image

Laravel Forge adds Statamic Integration

Read article
Transform Data into Type-safe DTOs with this PHP Package image

Transform Data into Type-safe DTOs with this PHP Package

Read article
PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell image

PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell

Read article
Herd Executable Support and Pest 3 Mutation Testing in PhpStorm 2024.3 image

Herd Executable Support and Pest 3 Mutation Testing in PhpStorm 2024.3

Read article