lundi 23 décembre 2019

Laravel 5.4 ,retrieve model that does not have any morph replations

I am trying to retrieve a polymorphic model that has null on its polymorphic columns. Based on the example below, how to retrieve comments that do not belong to any post or video

Example:

posts
    id - integer
    title - string
    body - text

videos
    id - integer
    title - string
    url - string

comments
    id - integer
    body - text
    commentable_id - integer
    commentable_type - string

Relationships:

class Comment extends Model
{
    public function commentable()
    {
        return $this->morphTo();
    }
}

class Post extends Model
{
    public function comments()
    {
        return $this->morphOne('App\Comment', 'commentable');
    }
}

class Video extends Model
{
    public function comments()
    {
        return $this->morphOne('App\Comment', 'commentable');
    }
}


via Chebli Mohamed

1 commentaire: