Getting Started with Laravel
The content of this page has not been updated to Strapi v5 yet.
This integration guide follows the Quick Start Guide and assumes you have you have fully completed the "Hands-on" path. You should be able to consume the API by browsing the URL http://localhost:1337/api/restaurants.
Should you wish to use standalone PHP, see the PHP integration guide.
This guide assumes you already have Laravel installed and are familiar with the basics of the framework.
Using the native Laravel Http Client
Following the official Laravel Macros documentation, you can make a Strapi Macro to integrate to the http
client from Laravel:
In App\Providers\AppServiceProvider
(or your ServiceProvider
):
use Illuminate\Support\Facades\Http;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Http::macro('strapi', function () {
return Http::withHeaders([
'Authorization' => 'Bearer '. config('strapi.token'), #Token generated in the admin
])->baseUrl(config('strapi.url')); # Base url of your strapi app
});
}
Create new config file for strapi in config/strapi.php
:
return [
'url' => env('STRAPI_URL'),
'token' => env('STRAPI_TOKEN', null),
];
Once your macro has been configured, you may invoke it from anywhere in your application to create a pending request with the specified configuration:
# Access to GraphQL
$response = Http::strapi()->post('graphql', ['query' => $gqlQuery, 'variables' => $variables]);
#Tip you might include a .gql file here using $gqlQuery = include('gqlQuery.gql')
# Access to Api Rest
$response = Http::strapi()->get('api/pages');
Install the Laravel-Strapi Laravel Package
composer require dbfx/laravel-strapi
This installs Laravel-Strapi, a Laravel specific package for interacting with Strapi.
You need to publish a config file:
php artisan vendor:publish --provider="Dbfx\LaravelStrapi\LaravelStrapiServiceProvider" --tag="strapi-config"
You also need to define your STRAPI_URL
and STRAPI_CACHE_TIME
in the .env
file:
STRAPI_URL=http://localhost:1337
STRAPI_CACHE_TIME=3600
Get your collection type
Execute a GET
request on the restaurant
collection type in order to fetch all your restaurants.
Be sure that you activated the find
permission for the restaurant
collection type.
$strapi = new Dbfx\LaravelStrapi();
$restaurants = $strapi->collection('restaurants');
You may now iterate over the $restaurants
array, which will contain all your restaurants. More options are available as well:
$restaurants = $strapi->collection('restaurants', $sortKey = 'id', $sortOrder = 'DESC', $limit = 20, $start = 0, $fullUrls = true);
Accessing single type items
You may also access single type items as follows:
$strapi = new Dbfx\LaravelStrapi();
// Fetch the full homepage array
$homepageArray = $strapi->single('homepage');
// Return just the ['content'] field from the homepage array
$homepageItem = $strapi->single('homepage', 'content');
Collection by field
$strapi = new Dbfx\LaravelStrapi();
$entries = $strapi->entriesByField('restaurants', 'slug', 'test-restaurant-name');
Single item from collection
$strapi = new Dbfx\LaravelStrapi();
$entry = $strapi->entry('restaurants', $id = 5);