Ich habe ein ähnliches System mit polymorphen Beziehungen gebaut. Ich möchte gerne das Modell des sympathischen_Typs beim Abrufen der Likes laden. Was muss ich tun? Ich versuchteLaravel 5.2 - Eager Last entfernt Modell in polymorpher Beziehung
protected $with = ['Post'];
oder
protected $with = ['App\Post'];
mein Wie Modell hinzufügen, aber alles, was ich bekommen ist:
Call to undefined Methode Illuminate \ Database \ Abfrage \ Builder :: Beitrag()
Wie Modell:
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();
}
}
Beitrag Modell:
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;
}
}
Zeigen Sie uns Ihr Modell. – Moak
Ich werde jetzt zur Frage hinzufügen. – Chriz74
Haben Sie 'protected $ with = ['likefable']' versucht? Normalerweise verweisen Sie beim Beantworten des Ladevorgangs auf die Beziehung mit dem Methodennamen – Moak