jjudge's avatar

Looking to add notes to any model

I'm wondering if anyone has any recommendations for a package that allows notes to be added to any model in an application. I'm guessing what I need is a polymorphic relationship, that can provide many child records for any other model, and probably a trait to add it to those models. It should be easy enough to write, but I am looking around to see if such a thing already exists so I can see it working practically.

Whether it just deals with the back end relationships, or has views and permissions for dealing with the front end too, I'm not too bothered about, just looking for an example.

Edit: actually, I think I need many-to-many polymorphic relationships for the notes, since a single note could be linked to multiple models that it is related to. I'm just going to go ahead and try that out and see how it looks and works.

Trying it out, I think I need the inverse relationship (notes to the models) to be dynamic, or defined by configuration rather than code. I think Oktober CMS manages to do something like that.

0 likes
4 replies
jjudge's avatar

Spot-on, thank you. I searched everywhere, but just didn't find the right terms to hit this one.

I started writing a package to do this last night, and it is very much along the same lines as this one, which is reassuring.

Just to expand a little on my use-case, the notes I am saving are mostly system-generated during some complex data processing tasks. I am wanting to use a many-to-many polymorphic relationship so that the context of each note can include the multiple models that are involved in the processing at each stage. The LaravelNotes package only has a one-to-many relationship, but it is easily extendable.

The many-to-many relationship is where the inverse relation comes into play. Given a single note on a model, being able to get a list of other instances of the same model that the note is also connected to, and other instances of other models that the note is connected to, will be very useful. I can't see how to do that that yet without hard-coding the relationships of the application models into the Note model. I'm sure there will be a way to declare the inverse relationships dynamically, or at least by configuration. Also, getting a collection of models connected to a Note, where different models entirely could be in the collection, is going to be interesting. But I guess polymorphic relationships will support that in Laravel without too much effort.

The polymorphic relationship is described here:

https://laravel.com/docs/5.5/eloquent-relationships#many-to-many-polymorphic-relations

In that example, getting tags for a post is easy: $post->tags and getting the posts for a tag is easy: $tag->posts (though with posts() hard-coded into the Tag model). Getting both posts and videos for a tag is the use-case not given, maybe looking something like $tag->allModels. I need to work that out.

jjudge's avatar
Level 9

I noticed support for https://github.com/ARCANEDEV/LaravelNotes stopped at laravel 9, and there don't seem to be any real alternatives (maybe a heavier "comments" module from Spatie, which is a difference beast). Has this package been abandoned now? Are people just writing their own?

1 like

Please or to participate in this conversation.