简体   繁体   中英

Get morphFrom on laravel polymorphic relation

I am using laravel polymorphic relationship

My Comment.php class

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    protected $fillable = ['body', 'commentable_id', 'commentable_type'];

    /**
     * Get the owning commentable model.
     */
    public function commentable()
    {
        return $this->morphTo();
    }    

    // public function commentFrom()
    // {
    //   return where it commented from.
    // }
}

What I want: I want all comment with where it commented (post/video) from.

只需将with('commentable')用于所有评论及其postvideo

Comment::with('commentable')->get()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM