/**
  * @return \Illuminate\Database\Query\Builder
  */
 protected function prepareQuery()
 {
     $query = $this->section->getEmptyDocument();
     foreach ($this->getJoins() as $join) {
         call_user_func_array([$query, array_shift($join)], $join);
     }
     return $query->getQuery();
 }
 /**
  * @param DocumentInterface     $document
  * @param SectionInterface|null $relatedSection
  * @param FieldInterface|null   $relatedField
  *
  * @return HasManyRelation
  */
 public function getDocumentRelation(DocumentInterface $document, SectionInterface $relatedSection = null, FieldInterface $relatedField = null)
 {
     $instance = $relatedSection->getEmptyDocument()->newQuery();
     $foreignKey = $this->getRelatedField()->getDBKey();
     $localKey = $relatedSection->getDocumentPrimaryKey();
     return new HasManyRelation($instance, $document, $foreignKey, $localKey);
 }
 /**
  * @param DocumentInterface     $document
  * @param SectionInterface|null $relatedSection
  * @param FieldInterface|null   $relatedField
  *
  * @return BelongsToRelation
  */
 public function getDocumentRelation(DocumentInterface $document, SectionInterface $relatedSection = null, FieldInterface $relatedField = null)
 {
     return new BelongsToRelation($relatedSection->getEmptyDocument()->newQuery(), $document, $this->getRelatedField()->getRelatedDBKey(), $this->getSection()->getDocumentPrimaryKey(), $this->getRelationName());
 }
 /**
  * @param DocumentInterface   $document
  * @param SectionInterface    $relatedSection
  * @param FieldInterface|null $relatedField
  *
  * @return \Illuminate\Database\Eloquent\Relations\ManyToMany
  */
 public function getDocumentRelation(DocumentInterface $document, SectionInterface $relatedSection = null, FieldInterface $relatedField = null)
 {
     $relatedDocument = $relatedSection->getEmptyDocument();
     $builder = $relatedDocument->newQuery();
     return new BelongsToManyRelation($builder, $document, $this->getRelatedTable(), $this->getDBKey(), $relatedField->getDBKey(), $this->getRelationName());
 }