PHP code example of tonysm / rich-text-laravel

1. Go to this page and download the library: Download tonysm/rich-text-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tonysm / rich-text-laravel example snippets


use Tonysm\RichTextLaravel\Models\Traits\HasRichText;

class Post extends Model
{
    use HasRichText;

    protected $guarded = [];

    protected $richTextAttributes = [
        'body',
        'notes',
    ];
}

use Tonysm\RichTextLaravel\Attributes\RichTextAttributes;
use Tonysm\RichTextLaravel\Models\Traits\HasRichText;

#[RichTextAttributes(['body', 'notes'])]
class Post extends Model
{
    use HasRichText;

    protected $guarded = [];
}

$post = Post::create(['body' => $body, 'notes' => $notes]);

$post->body->render();

$post->body->body->attachments();

$post->body->attachments();

// Loads all rich text fields (1 query for each field, since each has its own relationship)
Post::withRichText()->get();

// Loads only a specific field:
Post::withRichText('body')->get();

// Loads some specific fields (but not all):
Post::withRichText(['body', 'notes'])->get();

use Tonysm\RichTextLaravel\Models\Traits\HasRichText;

class Post extends Model
{
    use HasRichText;

    protected $guarded = [];

    protected $richTextAttributes = [
        'body' => ['encrypted' => true], // This will be encrypted...
        'notes', // Not encrypted...
    ];
}

#[RichTextAttributes(['body' => ['encrypted' => true], 'notes'])]
class Post extends Model
{
    use HasRichText;
}

namespace App\Providers;

use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\ServiceProvider;
use Tonysm\RichTextLaravel\RichTextLaravel;

class AppServiceProvider extends ServiceProvider
{
    // ...
    public function boot(): void
    {
        RichTextLaravel::encryptUsing(
            encryption: fn ($value, $model, $key) => Crypt::encrypt($value),
            decryption: fn ($value, $model, $key) => Crypt::decrypt($value),
        );
    }
}

use Tonysm\RichTextLaravel\Models\Traits\HasRichText;

class Post extends Model
{
    use HasRichText;

    protected $guarded = [];

    protected $richTextAttributes = [
        'body' => ['attribute' => true],
    ];
}

#[RichTextAttributes(['body' => ['attribute' => true]])]
class Post extends Model
{
    use HasRichText;
}

$post->body->toPlainText();
$post->body->attachments();

protected $richTextAttributes = [
    'body' => ['attribute' => true], // Stored in the model's own table...
    'notes', // Stored in the rich_texts table...
];

protected $richTextAttributes = [
    'body' => ['attribute' => true, 'encrypted' => true],
];

$post->body->attachments()

// Getting only attachments of users inside the rich text content.
$post->body->attachments()
    ->filter(fn (Attachment $attachment) => $attachment->attachable instanceof User)
    ->map(fn (Attachment $attachment) => $attachment->attachable)
    ->unique();

$post->body->links()

$post->body->attachmentGalleries()

$post->body->galleryAttachments()

$post->body->galleryAttachments()
    ->map(fn (Attachment $attachment) => $attachment->attachable)

use App\Models\Opengraph\OpengraphEmbed;
use Illuminate\Support\ServiceProvider;
use Tonysm\RichTextLaravel\RichTextLaravel;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        RichTextLaravel::withCustomAttachables(function (DOMElement $node) {
            if ($attachable = OpengraphEmbed::fromNode($node)) {
                return $attachable;
            }
        });
    }
}

namespace App\Models\Opengraph;

use DOMElement;
use Tonysm\RichTextLaravel\Attachables\AttachableContract;

class OpengraphEmbed implements AttachableContract
{
    const CONTENT_TYPE = 'application/vnd.rich-text-laravel.opengraph-embed';

    public static function fromNode(DOMElement $node): ?OpengraphEmbed
    {
        if ($node->hasAttribute('content-type') && $node->getAttribute('content-type') === static::CONTENT_TYPE) {
            return new OpengraphEmbed(...static::attributesFromNode($node));
        }

        return null;
    }

    // ...
}

$post->body->toPlainText()

$post->body->toMarkdown()

'editor' => env('RICH_TEXT_EDITOR', 'trix'),

'editors' => [
    'trix' => \Tonysm\RichTextLaravel\Editor\TrixEditor::class,
    'lexxy' => \Tonysm\RichTextLaravel\Editor\LexxyEditor::class,
],

namespace App\Editors;

use Tonysm\RichTextLaravel\Editor\Editor;
use Tonysm\RichTextLaravel\Fragment;

class MyCustomEditor implements Editor
{
    public function asCanonical(Fragment $fragment): Fragment
    {
        // Convert your editor's attachment format
        // into <rich-text-attachment> elements for storage.
        return $fragment;
    }

    public function asEditable(Fragment $fragment): Fragment
    {
        // Convert <rich-text-attachment> elements back
        // into your editor's native format for editing.
        return $fragment;
    }
}

'editors' => [
    'trix' => \Tonysm\RichTextLaravel\Editor\TrixEditor::class,
    'lexxy' => \Tonysm\RichTextLaravel\Editor\LexxyEditor::class,
    'my-editor' => \App\Editors\MyCustomEditor::class,
],
bash
php artisan richtext:install
bash
php artisan migrate
bash
php artisan vendor:publish --tag=rich-text-laravel-views