laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.21k stars 10.9k forks source link

[5.3.28] morphMany "has" methods not working on self relation #16861

Closed alexdanielyan closed 7 years ago

alexdanielyan commented 7 years ago

Description:

I have "Comment" Eloquent with morphMany and morphTo "comments". This relation not working for methods "has", "whereHas" and other. For example:

Comment::has('comments')->toSql()

gives query

select * from `comments` 
    where exists
        (select * from `comments` as `laravel_reserved_3`
            where `laravel_reserved_3`.`commentable_id` = `comments`.`id`
            and `comments`.`commentable_type` = ?)

In the last line should be laravel_reserved_3 instead comments

Steps To Reproduce:

  1. Create eloquent
    class Comment extends Model
    {
    public function comments()
    {
        return $this->morphMany('App\Comment', 'commentable');
    }
    public function commentable () {
        return $this->morphTo();
    }
    }
  2. Try
    Comment::has('comments')->toSql()
fernandobandeira commented 7 years ago

Check #5429, currently was closed down as a no-fix

alexdanielyan commented 7 years ago

I am completely disagree. All we need, redefine Illuminate\Database\Eloquent\Relations\HasOneOrMany.php line 89 getRelationQueryForSelfRelation in Illuminate\Database\Eloquent\Relations\MorphOneOrMany.php. There should be laravel_reserved_ for morp type column. I think it is possible.

themsaid commented 7 years ago

@team5ru can you submit a PR with some tests that shows the issue and how to fix?

alexdanielyan commented 7 years ago

Not in working time. On weekends.

themsaid commented 7 years ago

Closing this then since it was marked as no-fix before, but please submit a PR if you have a fix.