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',
];
}
// 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...
];
}
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\Casts\AsRichTextContent;
class Post extends Model
{
protected $casts = [
'body' => AsRichTextContent::class,
];
}
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()
bash
php artisan richtext:install
bash
php artisan migrate
blade
{!! $post->content !!}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.