Tony Messias

August 31, 2021

Rich Text Laravel 1.0.0-BETA

Hey, folks.

The Rich Text Laravel package is here. After some rewrites and some delay due to another package I needed in order to finish this one, it's time to get it out of development mode.

image.png


I've been tinkering with this package for a while. I got the idea of building this package when I was watching Chris Oliver's talk at RailsConf 2020 where he shows the powers of Rails' ActionText. I got fascinated. I knew Trix was powerful and deep down I knew ActionText wasn't just "OK, Rails ships with Trix now". But what he showed there blew my mind.

Enters Rich Text Laravel. It has a suggested database structure that keeps all Rich Text content in a separate table, outside of your models - similar to what ActionText does. This allows you to keep your models lean when working with them and only loads the Rich Text content when you actually need it. You can also eager load it if you know for sure that you will interact with it.

To keep it outside your model we have a RichText polymorphic model and we add dynamic relationships to your model when you use the `HasRichText` trait.

However, interacting with a relationship instead of a regular text field would require special handling just for the Rich Text content when compared to the other fields you may have on your model. The package adds what I'm calling "virtual fields" to your model to make the API a little better and closer to a regular field. These virtual fields have a caster associated with them to forward the calls to the respective Rich Text relationship for you.

So, instead of this:

$post = Post::create(['title' => request('title')]);

$post->richTextContent()->create([
    'body' => request('content'),
]);

You can keep using the regular field API:

$post = Post::create([
    'title' => request('title'),
    'content' => request('content'),
]);

Ain't this cool? I think so.

The package is in 1.0.0-BETA at the time of this writing. I'll give it a couple of days for y'all to try it out and report back before I tag 1.0.0. As I said, I don't foresee many deep changes.

There is a lot more about this package, such as:

  • Turn any model into an attachable
  • Extract all attachments from the rich text content
  • Extract all links from the rich text content
  • Convert the Rich Text Content to plain text with a method call

Check out the documentation about the Content object for more.

I plan to write some guides/tutorials with advanced scenarios of using the package. Give it a try and let me know what you think.

Do you want to receive updates? Make sure you subscribe here.

About Tony Messias