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

use App\Page;

$pages = Page::findByIds(['ID ONE', 'ID TWO']);

first

Return the first occurrence

use App\Page;

$page = Page::first();

all

Query all the documents of the page type.

use App\Page;

$pages = Page::all();

paginate

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

use App\Page;

$pages = Page::paginate(10);

where

use App\Page;

$page = Page::where('title', 'Test')->first();

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

whereIn

use App\Page;

$pages = Page::whereIn('title', ['Test'])->all();

whereNot

use App\Page;

$pages = Page::whereNot('title', 'Not Test')->all();

whereTag

Query the api based on a document tag.

use App\Page;

$pages = Page::whereTag('test')->all();

whereTags

Query the api based on multiple document tags.

use App\Page;

$pages = Page::whereTags(['test'])->all();

whereLanguage

use App\Page;

$pages = Page::whereLanguage('nl-NL')->all();

orderBy

use App\Page;

$pages = Page::orderBy('title')->all();

$pages = Page::orderBy('title desc')->all();

select

Specify the fields to select from the database.

$pages = Page::select('title', 'body')->all();

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.

class Page extends Model
{
    public function author()
    {
        return $this->hasOne(Author::class, 'author');
    }
}

$page = Page::with('author')->first();
$page->author // author object.

// Or without with
$page = Page::first();
$page->author() //author object.

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.

class Page extends Model
{
    public function authors()
    {
        return $this->hasMany(Author::class, 'authors', 'author');
    }
}

$pages = Page::with('authors')->all();

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.

class Page extends Model
{
    public function authors()
    {
        return $this->hasOne(['author' => Author::class, 'page' => Page::class], 'related_content');
    }
}

Eager loading

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

Note that only the fields specified are loaded.

class Page extends Model
{
    public function author()
    {
        return $this->hasOne(Author::class, 'author');
    }
}

$page = Page::with('author')->fetch('author.name', 'author.gender')->first();

$page->author->name;
$page->author->gender;

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

class Page extends Model
{
    public function articles()
    {
        foreach ($this->body as $slice) {
            // Primary fields
            if ($slice->type === 'my_defined_type') {
                $this->hasOne(Article::class, 'article', $slice->primary);
            }
            
            // Repeatable fields (items)
            if ($slice->type === 'my_defined_type') {
                $this->hasMany(Article::class, 'items', 'article' $slice);
            }
        }
    }
}

Last updated