Query builder

Basic query methods

Lets use the Page model from the previous chapter and query the api. We assume you created some base fields like a title, slug, and body.

single

Returns a model of a single content type.

use App\Homepage;

$homepage = Homepage::single();

find

First create a page with with the 'uid' field set to 'test' and publish it.

use App\Page;

$page = Page::find('test');

findById

use App\Page;

$page = Page::findById('YOUR PAGE ID');

findByIds

first

Return the first occurrence

all

Query all the documents of the page type.

paginate

The paginate methods works the same like the default Laravel paginate method.

where

Prismic Eloquent will automatically check if the field is a document based field or a user created field.

whereIn

whereNot

whereTag

Query the api based on a document tag.

whereTags

Query the api based on multiple document tags.

whereLanguage

orderBy

select

Specify the fields to select from the database.

Relationships

hasOne

You can specify a method on the model that returns a hasOne relation. The first parameter is the related model name and the second one the field. There is also a third parameter called parent. This can be used on fields like slices to specify any other object then the current model.

hasMany

You can specify a method on the model that returns a hasMany relation. The first parameter is the related model name and the second one the group name and the third one is the field.

Multiple models types

You can also define multiple model types since Prismic allows a content relation field to be of more then one content type.

Eager loading

Prismic also offers an option to eager load fields through the fetch links options.

Note that only the fields specified are loaded.

You can use the same method as above to load content inside slices.

Last updated