diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index ef7ac588dbd8..8370fafd6ea4 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -7,6 +7,7 @@ use Exception; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Concerns\BuildsQueries; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Pagination\Paginator; @@ -1143,7 +1144,11 @@ protected function parseWithRelations(array $relations) protected function createSelectWithConstraint($name) { return [explode(':', $name)[0], static function ($query) use ($name) { - $query->select(explode(',', explode(':', $name)[1])); + $query->select(array_map(static function ($column) use ($query) { + return $query instanceof BelongsToMany + ? $query->getRelated()->getTable().'.'.$column + : $column; + }, explode(',', explode(':', $name)[1]))); }]; }