Query builder
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.
Returns a model of a single content type.
use App\Homepage;
$homepage = Homepage::single();
First create a page with with the 'uid' field set to 'test' and publish it.
use App\Page;
$page = Page::find('test');
use App\Page;
$page = Page::findById('YOUR PAGE ID');
use App\Page;
$pages = Page::findByIds(['ID ONE', 'ID TWO']);
Return the first occurrence
use App\Page;
$page = Page::first();
Query all the documents of the page type.
use App\Page;
$pages = Page::all();
The paginate methods works the same like the default Laravel paginate method.
use App\Page;
$pages = Page::paginate(10);
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.
use App\Page;
$pages = Page::whereIn('title', ['Test'])->all();
use App\Page;
$pages = Page::whereNot('title', 'Not Test')->all();
Query the api based on a document tag.
use App\Page;
$pages = Page::whereTag('test')->all();
Query the api based on multiple document tags.
use App\Page;
$pages = Page::whereTags(['test'])->all();
use App\Page;
$pages = Page::whereLanguage('nl-NL')->all();
use App\Page;
$pages = Page::orderBy('title')->all();
$pages = Page::orderBy('title desc')->all();
Specify the fields to select from the database.
$pages = Page::select('title', 'body')->all();
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.
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();
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');
}
}
Prismic also offers an option to eager load fields through the fetch links options.
Not all fields are supported for eager loading (fetching). Please review this in the Prismic documentation.
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 modified 4yr ago