getForeignKey() public method

Get the default foreign key name for the model.
public getForeignKey ( ) : string
return string
 /**
  * Creates a translation.
  *
  * @param Model  $locale
  * @param string $text
  * @param Model  $parentTranslation
  *
  * @return Model
  */
 protected function firstOrCreateTranslation(Model $locale, $text, $parentTranslation = null)
 {
     // We'll check to see if there's a cached translation
     // first before we try and hit the database.
     $cachedTranslation = $this->getCacheTranslation($locale, $text);
     if ($cachedTranslation instanceof Model) {
         return $cachedTranslation;
     }
     // Check if auto translation is enabled. If so we'll run
     // the text through google translate and
     // save it, then cache it.
     if ($parentTranslation && $this->autoTranslateEnabled()) {
         $googleTranslate = new TranslateClient();
         $googleTranslate->setSource($parentTranslation->locale->code);
         $googleTranslate->setTarget($locale->code);
         try {
             $text = $googleTranslate->translate($text);
         } catch (ErrorException $e) {
             // Request to translate failed, set the text
             // to the parent translation
             $text = $parentTranslation->translation;
         } catch (UnexpectedValueException $e) {
             // Looks like something other than text was passed in,
             // we'll set the text to the parent translation
             // for this exception as well
             $text = $parentTranslation->translation;
         }
     }
     $translation = $this->translationModel->firstOrCreate([$locale->getForeignKey() => $locale->getKey(), $this->translationModel->getForeignKey() => isset($parentTranslation) ? $parentTranslation->getKey() : null, 'translation' => $text]);
     // Cache the translation so it's retrieved faster next time
     $this->setCacheTranslation($translation);
     return $translation;
 }
 /**
  * Aggregates data handling to the subclasses.
  *
  * @param  array       $data  the handling internediate data.
  * @param  array|Model $value the handling Model instance.
  * @return array              the resulting intermediate Format instance.
  */
 protected function aggregate(array $data, Model $value)
 {
     $data[self::KEY_OF_TABLE_NAME] = $value->getTable();
     $data[self::KEY_OF_FOREIGN_KEY] = $value->getForeignKey();
     $data[self::KEY_OF_OTHER_KEY] = $value->getOtherKey();
     return $data;
 }
 /**
  * @param \Illuminate\Database\Eloquent\Model $model
  * @param string $locale
  * @param string $alias
  * @return callable
  */
 protected function getJoinClause(Eloquent $model, $locale, $alias)
 {
     return function (JoinClause $join) use($model, $locale, $alias) {
         $primary = $model->getTable() . '.' . $model->getKeyName();
         $foreign = $model->getForeignKey();
         $langKey = $model->getLocaleKey();
         $join->on($alias . '.' . $foreign, '=', $primary)->where($alias . '.' . $langKey, '=', $locale);
     };
 }
 /**
  * @param \Illuminate\Database\Eloquent\Builder $builder
  * @param \Illuminate\Database\Eloquent\Model $model
  */
 protected function createWhere(EloquentBuilder $builder, Eloquent $model)
 {
     if ($model->getOnlyTranslated() && $model->shouldFallback()) {
         $key = $model->getForeignKey();
         $primary = "{$this->i18nTable}.{$key}";
         $fallback = "{$this->i18nTable}_fallback.{$key}";
         $ifNull = $builder->getQuery()->compileIfNull($primary, $fallback);
         $builder->whereRaw("{$ifNull} is not null");
     }
 }
Example #5
0
 /**
  * Build model dictionary keyed by the relation's foreign key.
  *
  * @param  \Illuminate\Database\Eloquent\Collection  $results
  * @return array
  */
 protected function buildDictionary(Collection $results)
 {
     $dictionary = array();
     $foreign = $this->farParent->getForeignKey();
     // First we will create a dictionary of models keyed by the foreign key of the
     // relationship as this will allow us to quickly access all of the related
     // models without having to do nested looping which will be quite slow.
     foreach ($results as $result) {
         $dictionary[$result->{$foreign}][] = $result;
     }
     return $dictionary;
 }
Example #6
0
 /**
  * Creates a translation.
  *
  * @param Model  $locale
  * @param string $text
  * @param Model  $parentTranslation
  *
  * @return Model
  */
 protected function firstOrCreateTranslation(Model $locale, $text, $parentTranslation = null)
 {
     // We'll check to see if there's a cached translation
     // first before we try and hit the database.
     $cachedTranslation = $this->getCacheTranslation($locale, $text);
     if ($cachedTranslation instanceof Model) {
         return $cachedTranslation;
     }
     // Check if auto translation is enabled. If so we'll run
     // the text through google translate and
     // save it, then cache it.
     if ($parentTranslation && $this->autoTranslateEnabled()) {
         $this->client->setSource($parentTranslation->locale->code);
         $this->client->setTarget($locale->code);
         try {
             $text = $this->client->translate($text);
         } catch (ErrorException $e) {
             // Request to translate failed, set the text
             // to the parent translation.
             $text = $parentTranslation->translation;
         } catch (UnexpectedValueException $e) {
             // Looks like something other than text was passed in,
             // we'll set the text to the parent translation
             // for this exception as well.
             $text = $parentTranslation->translation;
         }
     }
     if ($parentTranslation) {
         // If a parent translation is given we're looking for it's child translation.
         $translation = $this->translationModel->firstOrNew([$locale->getForeignKey() => $locale->getKey(), $this->translationModel->getForeignKey() => $parentTranslation->getKey()]);
     } else {
         // Otherwise we're creating the parent translation.
         $translation = $this->translationModel->firstOrNew([$locale->getForeignKey() => $locale->getKey(), 'translation' => $text]);
     }
     if (empty($translation->getAttribute('translation'))) {
         // We need to make sure we don't overwrite the translation
         // if it exists already in case it was modified.
         $translation->setAttribute('translation', $text);
     }
     if ($translation->isDirty()) {
         $translation->save();
     }
     // Cache the translation so it's retrieved faster next time
     $this->setCacheTranslation($translation);
     return $translation;
 }
Example #7
0
 /**
  * Join pivot or 'through' table.
  *
  * @param  \Illuminate\Database\Eloquent\Model $parent
  * @param  \Illuminate\Database\Eloquent\Relations\Relation $relation
  * @param  string $type
  * @return void
  */
 protected function joinIntermediate(Model $parent, Relation $relation, $type)
 {
     if ($relation instanceof BelongsToMany) {
         $table = $relation->getTable();
         $fk = $relation->getForeignKey();
     } else {
         $table = $relation->getParent()->getTable();
         $fk = $table . '.' . $parent->getForeignKey();
     }
     $pk = $parent->getQualifiedKeyName();
     if (!$this->alreadyJoined($join = (new Join($type, $table))->on($fk, '=', $pk))) {
         $this->query->joins[] = $join;
     }
 }
 /**
  * set the clauses of the sql from the ids array
  * for a where in statement
  * 
  * @param  array  $ids
  * @return null
  */
 public function only(array $ids)
 {
     $tableName = $this->model->getTable();
     $this->setJoin([$tableName, $this->table . '.' . $this->model->getForeignKey(), '=', $tableName . '.id']);
     $this->setWhereIn([$tableName . '.id', $ids]);
 }