@@ -51,19 +51,21 @@ public function scopeNotTranslatedIn(Builder $query, ?string $locale = null)
5151 public function scopeOrderByTranslation (Builder $ query , string $ translationField , string $ sortMethod = 'asc ' )
5252 {
5353 $ translationTable = $ this ->getTranslationsTable ();
54+ $ localeKey = $ this ->getLocaleKey ();
5455 $ table = $ this ->getTable ();
56+ $ keyName = $ this ->getKeyName ();
5557
5658 $ hasLeftJoin = collect ($ query ->getQuery ()->joins ?? [])
5759 ->contains (fn ($ join ) => $ join instanceof JoinClause && $ join ->table === $ translationTable );
5860
5961 return $ query
6062 ->with ('translations ' )
6163 ->select ("{$ table }.* " )
62- ->unless ($ hasLeftJoin , function (Builder $ query ) use ($ translationTable , $ table ) {
63- $ query ->leftJoin ($ translationTable , function (JoinClause $ join ) use ($ translationTable , $ table ) {
64+ ->unless ($ hasLeftJoin , function (Builder $ query ) use ($ translationTable , $ localeKey , $ table, $ keyName ) {
65+ $ query ->leftJoin ($ translationTable , function (JoinClause $ join ) use ($ translationTable , $ localeKey , $ table, $ keyName ) {
6466 $ join
65- ->on ("{$ translationTable }. {$ this ->getTranslationRelationKey ()}" , '= ' , "{$ table }. {$ this -> getKeyName () }" )
66- ->where ("{$ translationTable }. {$ this -> getLocaleKey () }" , $ this ->locale ());
67+ ->on ("{$ translationTable }. {$ this ->getTranslationRelationKey ()}" , '= ' , "{$ table }. {$ keyName }" )
68+ ->where ("{$ translationTable }. {$ localeKey }" , $ this ->locale ());
6769 });
6870 })
6971 ->orderBy ("{$ translationTable }. {$ translationField }" , $ sortMethod );
0 commit comments