[英]Laravel 5.2 - Eager load distant model in Polymorphic relation
I have a like system built with Polymorphic relations.我有一个用多态关系构建的类似系统。 I'd like to eager load the model of the likeable_type when retrieving the Likes.我想在检索 Likes 时急切加载 likeable_type 的模型。 What do I need to do?我需要做什么? I tried to add我试着添加
protected $with = ['Post'];
or或者
protected $with = ['App\Post'];
to my Like model but all I get is:到我的 Like 模型,但我得到的只是:
Call to undefined method Illuminate\\Database\\Query\\Builder::Post()调用未定义的方法 Illuminate\\Database\\Query\\Builder::Post()
Like model:喜欢的型号:
class Like extends Model
{
use SoftDeletes;
protected $table = 'likeables';
protected $fillable = [
'user_id',
'likeable_id',
'likeable_type',
];
/**
* Get all of the owning likeable models.
*/
public function likeable()
{
return $this->morphTo();
}
}
Post Model:岗位型号:
class Post extends Model
{
protected $fillable = [
'picture',
'description'
];
/**
*
* a post belongs to one user
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user(){
return $this->belongsTo('App\User');
}
/**
*
* a post has many comments
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function comments()
{
return $this->hasMany('App\Comment');
}
/**
* Get all of the post's likes.
*/
public function likes()
{
return $this->morphMany('App\Like', 'likeable');
}
/**
*
* Simple Like System checks if liked
*
* @return bool
*/
public function getIsLikedAttribute()
{
$like = $this->likes()->whereUserId(Auth::id())->first();
return (!is_null($like)) ? true : false;
}
}
I solved this issue by renaming the table from 'likeables' to 'likes'.我通过将表从“likeables”重命名为“likes”解决了这个问题。
protected $table = 'likeables';
-> protected $table = 'likes';
-> protected $table = 'likes';
Now I can retrieve the distant relation when retrieving the like like this:现在我可以在检索类似这样的东西时检索远方关系:
$post = Post::find(6);
dd ($post->likes()->with('likeable')->get());
or I can retrieve the liked entity like this:或者我可以像这样检索喜欢的实体:
$like = Like::find(1);
$likeable = $like->likeable;
dd($likeable);
before renaming the table the result was Null.. I still have to understand how to retrieve all the entities liked by a User.在重命名表之前,结果为 Null .. 我仍然需要了解如何检索用户喜欢的所有实体。
Illuminate\\Database\\Query\\Builder::Post() Illuminate\\Database\\Query\\Builder::Post()
You have a problem about post.你有一个关于帖子的问题。
You can take datas like this with "firstOrFailed()":您可以使用“firstOrFailed()”获取这样的数据:
$data=table_name::where('$id','like',$id)->firstOrFailed(); $data=table_name::where('$id','like',$id)->firstOrFailed();
Or "get()" method.或“get()”方法。 (This changes with releations.) (这会随着关系而变化。)
This is the tutorial about polimorphic releations.这是关于多态关系的教程。 https://laravel.com/docs/5.3/eloquent-relationships#polymorphic-relations https://laravel.com/docs/5.3/eloquent-relationships#polymorphic-relations
If you can't solve the problem, please give more information like controller, view page and migration.如果您不能解决问题,请提供更多信息,如控制器、查看页面和迁移。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.