2021-09-18
1682
#laravel
Adewale Abati
3921
Sep 18, 2021 ⋅ 6 min read

Polymorphic relationships in Laravel and their use cases

Adewale Abati Web engineer, tech lifestyle YouTuber, public speaker. Building communities and open source for the Next Billion Users.

Recent posts:

Radix Ui Adoption Guide Overview Examples And Alternatives

Radix UI adoption guide: Overview, examples, and alternatives

Radix UI is quickly rising in popularity and has become an excellent go-to solution for building modern design systems and websites.

Nefe Emadamerho-Atori
Apr 25, 2024 ⋅ 11 min read
Understanding The Css Revert Layer Keyword, Part Of Css Cascade Layers

Understanding the CSS revert-layer keyword

In this article, we’ll explore CSS cascade layers — and, specifically, the revert-layer keyword — to help you refine your styling strategy.

Chimezie Innocent
Apr 24, 2024 ⋅ 6 min read
Exploring Nushell, A Rust Powered, Cross Platform Shell

Exploring Nushell, a Rust-powered, cross-platform shell

Nushell is a modern, performant, extensible shell built with Rust. Explore its pros, cons, and how to install and get started with it.

Oduah Chigozie
Apr 23, 2024 ⋅ 6 min read
Exploring Zed, A Newly Open Source Code Editor Written In Rust

Exploring Zed, an open source code editor written in Rust

The Zed code editor sets itself apart with its lightning-fast performance and cutting-edge collaborative features.

Nefe Emadamerho-Atori
Apr 22, 2024 ⋅ 7 min read
View all posts

22 Replies to "Polymorphic relationships in Laravel and their use cases"

  1. Thanks for the great explanation. It was very easy to learn this content here.

    I have 2 questions:

    1. Are you missing S on $page->comment(s)? And in other loops too?
    foreach($page->comment as $comment)
    {
    // working with comment here…
    }

    2. In which column the comments are stored in comments table? Because we have only: Id, commendable_id, commendable_type and date.

  2. 2. Its an error in this article – in comments migrations we saw $table->date(‘body’); .. then must by $table->string(‘body’); or $table->text(‘body’); – body column is for the coment content 🙂

  3. Great article. I have one question: how would you return the inverse? Eg all comments of class Page?

  4. This was exactly what I was looking for. All my scenarios were discussed here. This is fantastic. Thank you very much.

  5. Hi, greate article!!!
    Just one small mistake: it should be $table→morphs(‘commentable’) not $table→morphs(‘comment’) which would automatically create two columns using the text passed to (it won’t add able, atleast not in L8). So it will result in commentable_id and commentable_type.

  6. this is useless if you not going to teach actionable events like attaching comment to post or sync without detaching!! stop supporting half baked articles

  7. The way you’ve explained this complex concept is truly impressive. Laravel’s flexibility never ceases to amaze me, and this article really highlights the power of polymorphic relationships in making our code cleaner and more efficient.

Leave a Reply