Skip to content

Commit

Permalink
Add soft delete condition to scope with translation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tihomir Banov committed Jun 4, 2015
1 parent ee1a5fe commit b62f4ab
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions src/Despark/LaravelDbLocalization/i18nModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,35 @@ public function translate($locale = false)

public function scopeWithTranslations($query, $locale = null, $softDelete = null)
{
// get i18n id by locale
$i18nId = $this->getI18nId($locale);

$translatorTable = new $this->translator();
$translatorTableName = $translatorTable->getTable();
$transfield = $this->getTranslatorField();
$table = $this->getTable();
$translatableTable = $this->getTable();

$translatorField = $this->getTranslatorField();
$localeField = $this->getLocaleField();

if (! $locale) {
$query = $query->leftJoin(
$translatorTableName,
$translatorTableName.'.'.$transfield, '=', $table.'.id');
$translatorTableName.'.'.$translatorField, '=', $translatableTable.'.id');
} else {
$aliasSoftDelete = '';
if ($softDelete) {
$aliasSoftDelete = 'AND translatorAlias.deleted_at is null ';
}

$query = $query->leftJoin(\DB::raw(
'( SELECT
translatorAlias.*
FROM '.$translatorTableName.' as translatorAlias
WHERE translatorAlias.'.$localeField.' = '.$i18nId.'
'.$aliasSoftDelete.'
) as '.$translatorTableName
), function ($join) use ($translatorTableName, $transfield, $table) {
$join->on($translatorTableName.'.'.$transfield, '=', $table.'.id');
), function ($join) use ($translatorTableName, $translatorField, $translatableTable) {
$join->on($translatorTableName.'.'.$translatorField, '=', $translatableTable.'.id');
});
}

Expand All @@ -142,20 +151,6 @@ public function scopeWithTranslations($query, $locale = null, $softDelete = null
return $query;
}

/**
* Create new record.
*
* @param array $attributes
*/
public static function create(array $attributes)
{
$model = new static($attributes);

$model->save($attributes);

return $model;
}

/**
* Save record.
*
Expand Down

0 comments on commit b62f4ab

Please sign in to comment.