Eloquent eager load Order by

后端 未结 7 2114
花落未央
花落未央 2020-11-27 13:56

I have problem with eloquent query. I am using eager loading (one to one Relationship) to get \'student\' With the \'exam\', Using the code

7条回答
  •  無奈伤痛
    2020-11-27 14:39

    You could use \Illuminate\Database\Eloquent\Relations\Relation and query scopes to add far column through relationship, I wrote a traits for this, it misses HasOne o HasMany but having BelongsTo and BelongsToMany could easily adapted

    Also the method could be enhanced to support more than depth 1 for multiple chained relationship, I made room for that

    $relationName();
                } else {
                    throw new BadMethodCallException("Relationship $relationName does not exist, cannot join.");
                }
                $currentTable = $currentModel->getTable();
                if ($relationIndex == 0) {
                    $query->addSelect($currentTable . '.*');
                }
                $relatedModel = $relation->getRelated();
                /**
                 * @var string
                 */
                $relatedTable = $relatedModel->getTable();
    
                if ($relation instanceof BelongsTo) {
                    foreach ($columns as $alias => $column) {
                        $tableAlias = $tableAliasPrefix . $relationIndex;
                        $tableAndAlias = $relatedTable . ' AS ' . $tableAlias;
                        /**
                         * Al momento gestisce soltanto la prima relazione
                         * todo: navigare le far relationships e creare delle join composte
                         */
                        if (!isset($subQueries[$alias])) {
                            $subQueries[$alias] = $currentQuery = DB::query()
                                ->from($tableAndAlias)
                                ->whereColumn(
                                    $relation->getQualifiedForeignKey() , // 'child-table.fk-column'
                                    '=' ,
                                    $tableAlias . '.' . $relation->getOwnerKey()  // 'parent-table.id-column'
                                )
                                ->select($tableAlias . '.' . $column);
                            // se la colonna ha una chiave stringa e' un alias
                            /**
                             * todo: in caso di relazioni multiple aggiungere solo per la piu lontana
                             */
                            if (is_string($alias)) {
                                $query->selectSub($currentQuery , $alias);
                            } else {
                                throw new \InvalidArgumentException('Columns must be an associative array');
                            }
                        } 
                        else {
                            throw new \Exception('Multiple relation chain not implemented yet');
                        }
                    } // end foreach 
                } // endif
                else if ($relation instanceof BelongsToMany) {
                    foreach ($columns as $alias => $column) {
    
                        $tableAlias = $tableAliasPrefix . $relationIndex;
                        $tableAndAlias = $relatedTable . ' AS ' . $tableAlias;
    
                        if (!isset($subQueries[$alias])) {
                            $pivotTable = $relation->getTable();
                            $subQueries[$alias] = $currentQuery = DB::query()
                                ->from($tableAndAlias)
                                ->select($tableAlias . '.' . $column)
                                // final table vs pivot table
                                ->join(
                                    $pivotTable ,                               // tabelle pivot
                                    $relation->getQualifiedRelatedKeyName() ,    // pivot.fk_related_id
                                    '=' ,
                                    $tableAlias . '.' . $relatedModel->getKeyName() // related_with_alias.id
                                )
                                ->whereColumn(
                                    $relation->getQualifiedForeignKeyName() ,
                                    '=' ,
                                    $relation->getParent()->getQualifiedKeyName()
                                );
    
                            if (is_string($alias)) {
                                $query->selectSub($currentQuery , $alias);
                            } else {
                                throw new \InvalidArgumentException('Columns must be an associative array');
                            }
                        } 
                        else {
                            throw new \Exception('Multiple relation chain not implemented yet');
                        }
                    } // end foreach 
                } else {
                    throw new \InvalidArgumentException(
                        sprintf("Relation $relationName of type %s is not supported" , get_class($relation))
                    );
                }
                $currentModel = $relatedModel;
                $relationIndex++;
            } // end foreach 
        }
    
        /**
         * @param $length
         * @return string
         */
        public static function randomStringAlpha($length) {
            $pool = array_merge(range('a', 'z'),range('A', 'Z'));
            $key = '';
            for($i=0; $i < $length; $i++) {
                $key .= $pool[mt_rand(0, count($pool) - 1)];
            }
            return $key;
        }
    }
    

提交回复
热议问题