PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Come usare inner join con subquery in Laravel Eloquent

ha funzionato (ignora le cose dinamiche come questa->getClassName ecc.). la logica di base funziona perfettamente

public function scopeAddTranslations($query)
{
    $t = new Translation();

    $subq = $t->select('item','text as ref_ar')
              ->where('locale','=','ar')
              ->where('item','like',$this->getClassName().'.ref%');

    $query->leftjoin(\DB::raw('('.$subq->toSql().') as t'), 
      function ($join) use ($subq) { 
          $join->on(\DB::raw('SUBSTRING('.$this->getTable().'.ref_translation 
                              FROM 14 FOR 26)'),
                                 '=',
                                 \DB::raw('t.item'))
                   ->addBinding($subq->getBindings());
            });
    return $query;
}