从laravel多态关系中获取变形
我正在使用 My从laravel多态关系中获取变形,laravel,eloquent,polymorphic-associations,Laravel,Eloquent,Polymorphic Associations,我正在使用 MyComment.phpclass namespace App; use Illuminate\Database\Eloquent\Model; class Comment extends Model { protected $fillable = ['body', 'commentable_id', 'commentable_type']; /** * Get the owning commentable model. */ pu
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.
// }
}
我想要什么:我想要所有的评论,包括它评论(帖子/视频)的来源。只需使用和('commentable')
来查看所有评论及其帖子
或视频
:
Comment::with('commentable')->get()
以及post
和video
之间的关系在哪里?你也可以完成where条件吗?@hasan05你想得到有视频或post的评论吗?我想用视频或post获得所有评论。@hasan05抱歉,我误解了,我已经将代码改为一对多