# Getting Started with Laravel

This integration guide is following the Quick Start Guide. We assume that you have fully completed its "Hands-on" path, and therefore can consume the API by browsing this url (opens new window).

Should you wish to use standalone PHP, you may also be interested in the PHP integration guide.

This guide assumes you already have Laravel installed (opens new window) and are familiar with the basics of the framework.

# Using the native Laravel Http Client

Following the official Laravel documentation, you can easily make a Strapi Macro to integrated it to the Http client from Laravel :

https://laravel.com/docs/9.x/http-client#macros :

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 '.env('STRAPI_TOKEN'), #Token generated in the admin
        ])->baseUrl(env('STRAPI_URL')); # Base url of your strapi app
    });
}

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

✋ CAUTION

This third-party integration guide might not be up-to-date with Strapi v4. Contributions (opens new window) are most welcome.

composer require dbfx/laravel-strapi

This will install Laravel-Strapi (opens new window), a Laravel specific package for interacting with Strapi.

You will need to publish a config file:

php artisan vendor:publish --provider="Dbfx\LaravelStrapi\LaravelStrapiServiceProvider" --tag="strapi-config"

You will 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.

Example GET request

$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-restuarant-name');

# Single item from collection

$strapi = new Dbfx\LaravelStrapi();
$entry = $strapi->entry('restaurants', $id = 5);

# Conclusion

Here is how to request your collection types in Strapi using Laravel. When you create a collection type or a single type you will have a certain number of REST API endpoints available to interact with.

There is more documentation available in the README (opens new window) or in the PHP integration guide.