Automatic Join on Eloquent Models with relations setup

Submitted by phazei - 9 years ago

Since Eloquent doesn't do a real join when using "with" this will add a simple join method without having to worry about the join columns since it's all stored with the models anyway. It will also select all the columns from the joined table with an added prefix of the table name.

    /**
     * This determines the foreign key relations automatically to prevent the need to figure out the columns.
     *
     * @param \Illuminate\Database\Query\Builder $query
     * @param string $relation_name
     * @param string $operator
     * @param string $type
     * @param bool   $where
     * @return \Illuminate\Database\Query\Builder
     */
    public function scopeModelJoin($query, $relation_name, $operator = '=', $type = 'left', $where = false) {
        $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);

    }