One video from my course "Eloquent: Expert Level": laraveldaily.t... Polymorphic relations are one of the most difficult concept to understand for junior developers, and I tried to do it with a real-life example.
Thank you so much!!! Was able to get the point of Polymorphic right of the bat. Definitely worth it. Hoping I could take your course but it seems its too expensive for me at the moment.
Thank you, your explanation save my day, I was very confused at beginning and I watched this lesson and debug many times until got the trick how it works under the hood.
Nicely explained. So if we have 3 tables like posts, products and images then we can make polymorphic relationship to bring data in optimized way. This can be done if we create two photo tables one is for post_photos and another is product_photos but for polymorphic relation we can reduce one table here. Thanks.
Thanks, I have one question how will we reserve it means Photo::get() and It should fetch relationship data from relevant table example post or product.
i was confused on this i am doing it in rails its different code wise but im assuming its the same concept thank you for the video and for the excellent explanation
how to update? i have Polymorphic tags, and i can store success but i cant update fine, the result contact the tags,for example #hello #word and i add , #one.. the result in my input is #hello #word #one #hello #word #one my code.. $tags_name = explode(' , ' , $request->tags_name); foreach($tags_name as $tag_name) { $post->tags()->attach(Tag::updateOrCreate(['name'=>$tag_name])); }
What is the error that you're getting? Maybe you shouldn't to ->attach() but instead create things manually, like PostTag::create('taggable_id' => $post_id) ... etc? (not sure what your polymorphic model is called, assumg PostTag)
Hi, thx for the answer, My problem is that I don't think I'm using the most optimal solution to update the registry, since I'm removing the tags with dettach and then adding them again ... es.stackoverflow.com/questions/418957/como-hacer-un-update-en-una-tabla-polimorfica,
But if my Models should have only one photo per record, having regular BelongsTo would be totally enough. Am I right? Posts ---id ---text ---image_id Users ---id ---name ---image_id Images ---id ---filename Or there are still advantages to use Polymorphic Relations in this case?
If you have only those models strictly, then yes, your structure is good. But if in the future you're planning to have, for example, Comments with image_id or something else with image_id, polymorphic structure would allow you to achieve those new fields without changing any structure of DB cause you would reuse the same images database table.