【问题标题】:Get morphFrom on laravel polymorphic relation在 laravel 多态关系上获取 morphFrom
【发布时间】:2020-02-07 04:57:22
【问题描述】:

我正在使用laravel polymorphic relationship

我的Comment.php 班级

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.
    // }
}

我想要什么:我想要所有评论,以及评论(帖子/视频)的来源。

【问题讨论】:

    标签: laravel eloquent polymorphic-associations


    【解决方案1】:

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

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

    【讨论】:

    • postvideo 的关系在哪里?
    • 你也能完成where条件吗?
    • @hasan05 你想得到有视频或帖子的评论吗?
    • 我想通过它的视频或帖子获得所有评论。
    • @hasan05 对不起,我误会了,我把代码改成了多态一对多
    猜你喜欢
    • 2019-06-17
    • 2020-09-10
    • 1970-01-01
    • 2019-07-17
    • 2021-05-06
    • 2020-10-13
    • 2015-04-05
    • 1970-01-01
    • 2015-10-24
    相关资源
    最近更新 更多