-
-
Save fredyounan/b384aad772b8192dd8e55ded6d0dd3dd to your computer and use it in GitHub Desktop.
Forces eloquent to join tables instead of eager-loading. useful for filtering & orderin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param $query | |
| * @param $relation_name | |
| * @param string $operator | |
| * @param string $type | |
| * @param bool $where | |
| * @return mixed | |
| */ | |
| public function scopeModelJoin($query, $relation_name, $operator = '=', $type = 'left', $where = false) | |
| { | |
| /** @var \Illuminate\Database\Eloquent\Relations\Relation $relation */ | |
| $relation = $this->$relation_name(); | |
| $table = $relation->getRelated()->getTable(); | |
| $one = $relation->getQualifiedParentKeyName(); | |
| $two = $relation->getForeignKey(); | |
| if (empty($query->columns)) { | |
| $query->select($this->getTable().".*"); | |
| } | |
| foreach (\Schema::getColumnListing($table) as $related_column) { | |
| $query->addSelect(new Expression("`$table`.`$related_column` AS `$table.$related_column`")); | |
| } | |
| return $query->join($table, $one, $operator, $two, $type, $where); //->with($relation_name); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment