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 = [];
}
// 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;
}
protected $richTextAttributes = [
'body' => ['attribute' => true], // Stored in the model's own table...
'notes', // Stored in the rich_texts table...
];
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;
}
// ...
}
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;
}
}