mbsoft31 / laravel-openalex
A fluent, elegant, and modern wrapper for the OpenAlex API, built for Laravel.
Fund package maintenance!
Mbsoft
Requires
- php: ^8.4
- illuminate/contracts: ^11.0||^12.0
- spatie/laravel-data: ^4.17
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
README
A fluent, elegant, and modern wrapper for the OpenAlex API, built for Laravel.
Installation
composer require mbsoft31/laravel-openalex
Next, publish the configuration file:
php artisan vendor:publish \--provider="Mbsoft\\OpenAlex\\OpenAlexServiceProvider" \--tag="config"
It is highly recommended to add your email to config/openalex.php.
Usage
Basic Queries
use Mbsoft\\OpenAlex\\Facades\\OpenAlex; // Find a work by its OpenAlex ID $work = OpenAlex::works()->find('W2741809807'); // Find an author by their ORCID $author = OpenAlex::authors()->findByOrcid('0000-0002-1825-0097'); // Get recent, highly-cited works about AI from a specific institution $works = OpenAlex::works() ->whereInstitutionRor('042nb2s44') // MIT ->where('publication_year', '>2024') ->search('artificial intelligence') ->sortBy('cited_by_count') ->get();
Automatic Pagination
The paginate
method returns a standard Laravel LengthAwarePaginator
instance.
$paginatedWorks = OpenAlex::works() ->whereHas('concepts.id', 'C15744967') // AI ->paginate(perPage: 50, page: 2); echo "Total AI papers: " . $paginatedWorks->total();
Cursors for Large Datasets
Use cursor()
to iterate over a large result set without consuming much memory. It fetches pages on-demand in the background.
$allWorksFromJournal = OpenAlex::works() ->where('primary_location.source.id', 'S4306520188') ->cursor(); foreach ($allWorksFromJournal as $work) { // Process millions of records safely }
Caching
Drastically improve performance by caching API responses.
// Cache for 10 minutes $works = OpenAlex::works()->where('publication_year', 2025)->cacheFor(600)->get(); // Cache forever $source = OpenAlex::sources()->find('S139121223')->cacheForever()->get();
DTO Utilities
The returned Data Transfer Objects are equipped with helpful methods.
$work = OpenAlex::works()->find('W2741809807'); // Get plain text abstract $abstract = $work->getAbstract(); // Get BibTeX citation $bibtex = $work->toBibTeX();