https://github.com/dbfx/laravel-strapi
Laravel wrapper for using the Strapi headless CMS
https://github.com/dbfx/laravel-strapi
hacktoberfest laravel laravel-package strapi strapi-cms
Last synced: 5 months ago
JSON representation
Laravel wrapper for using the Strapi headless CMS
- Host: GitHub
- URL: https://github.com/dbfx/laravel-strapi
- Owner: dbfx
- License: mit
- Created: 2021-04-27T07:14:32.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2025-04-20T04:48:25.000Z (about 1 year ago)
- Last Synced: 2025-10-02T13:32:46.143Z (8 months ago)
- Topics: hacktoberfest, laravel, laravel-package, strapi, strapi-cms
- Language: PHP
- Homepage: https://blakey.co
- Size: 86.9 KB
- Stars: 48
- Watchers: 3
- Forks: 33
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# Laravel wrapper for using the Strapi headless CMS
[](https://packagist.org/packages/dbfx/laravel-strapi)
[](https://packagist.org/packages/dbfx/laravel-strapi)
---
Laravel-Strapi is a Laravel helper for using the Strapi headless CMS.
Note: for Strapi v3 support use version 2.x.x
## Installation
You can install the package via composer:
```bash
composer require dbfx/laravel-strapi
```
You can publish and run the migrations with:
You can publish the config file with:
```bash
php artisan vendor:publish --provider="Dbfx\LaravelStrapi\LaravelStrapiServiceProvider" --tag="strapi-config"
```
You need to define your STRAPI_URL and STRAPI_CACHE_TIME in .env:
You can also optionally define a STRAPI_TOKEN to enable authentication. Do not include 'Bearer' only the token itself.
```
STRAPI_URL=https://strapi.test.com
STRAPI_CACHE_TIME=3600
STRAPI_TOKEN=abcd1234abcd1234
```
## Usage
laravel-strapi provides the collection() and entry() calls to return a full collection, or a specific entry from a collection. In the
example below we are querying the strapi collection 'blogs' and then getting the entry with id 1 from that collection.
```php
use Dbfx\LaravelStrapi\LaravelStrapi;
$strapi = new LaravelStrapi();
$blogs = $strapi->collection('blogs');
$entry = $strapi->entry('blogs', 1);
```
There are several useful options available as well.
- ```$sortKey``` and ```$sortOrder``` allow you to specify the key to sort on and the direction
- ```$fullUrls``` will automatically add your STRAPI_URL to the front of any relative URLs (e.g. images, etc).
- ```$limit``` sets how many items you are requesting
- ```$start``` is the offset to be used with limit, useful for pagination
- ```$populate``` is an array containing the fields to populate
- ```$queryData``` is an array of additional key-value pairs to add to the query string
```php
use Dbfx\LaravelStrapi\LaravelStrapi;
$strapi = new LaravelStrapi();
$blogs = $strapi->collection('blogs', $sortKey = 'id', $sortOrder = 'DESC', $limit = 20, $start = 0, $fullUrls = true, $populate = ['author', 'images'], $queryData = ['locale' => 'en']);
$entry = $strapi->entry('blogs', 1, $fullUrls = true, $populate = ['author', 'images'], $queryData = ['locale' => 'en']);
```
You may also access Single Type items as follows:
```php
use Dbfx\LaravelStrapi\LaravelStrapi;
$strapi = new LaravelStrapi();
// Fetch the full homepage array
$homepageArray = $strapi->single('homepage');
// Return just the ['content'] field from the homepage array
$homepageItem = $strapi->single('homepage', 'content');
```
And you may select entries by searching for a custom field (e.g. slug):
```php
use Dbfx\LaravelStrapi\LaravelStrapi;
$strapi = new LaravelStrapi();
$entries = $strapi->entriesByField('blogs', 'slug', 'test-blog-post');
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Credits
- [Dave Blakey](https://github.com/dbfx)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.