John Davidson

php - Can't get my Polymorphic relationships working

0 comments
Message:


couldn't find any answer on stackoverflow yet, so imma ask it by myself. My polymorphic relationships work kinda weird. I cannot retrieve Comments from the post, but I can retrieve Post from the specific Comment.


My comment:



And here is my code for relationships:


(I am using Thread model as well, but it isn't important in this situation).


POST:


public function comments() {
return $this->morphMany(Comment::class, 'commentable');
}

COMMENT:


public function commentable() {
return $this->morphTo();
}

Websites output when I use $comments = Post::where('id', 1)->first()->comments; and then dd($comments) :



EDIT:
It seems like adding the comment right on the page helped. Whereas adding the comment manually in phpmyadmin caused problems. Problem solved.



My Answer:
you please provide more context or clarify your question?

Rate this post

4 of 5 based on 5300 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us