Illuminate\Database\Eloquent\Model::getForeignKey PHP Method

getForeignKey() public method

Get the default foreign key name for the model.
public getForeignKey ( ) : string
return string
    public function getForeignKey()
    {
        return Str::snake(class_basename($this)) . '_' . $this->primaryKey;
    }

Usage Example

 /**
  * 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;
 }
All Usage Examples Of Illuminate\Database\Eloquent\Model::getForeignKey
Model