From db4e2d420760cd3f6142b210431883587b1f2104 Mon Sep 17 00:00:00 2001 From: Diogo Gomes Date: Wed, 13 Mar 2024 15:06:35 +0000 Subject: [PATCH] Add method to show field (#79) * add method to render the field's content * add new method to docs --- README.md | 9 +++++++++ src/Traits/HasTrixRichText.php | 5 +++++ tests/Feature/HasTrixRichTextTest.php | 12 ++++++++++++ 3 files changed, 26 insertions(+) diff --git a/README.md b/README.md index c80200e..8574b9e 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,15 @@ there's multiple ways to render trix for already existing model {!! app('laravel-trix')->make($post, 'content') !!} ``` +### Render Html For Existing Model + +You can render the html content for already existing model + +```php + + +{!! $post->trixRender('content') !!} //must use HasTrixRichText trait in order for $model->trixRender() method work +``` ### Storing Attachment diff --git a/src/Traits/HasTrixRichText.php b/src/Traits/HasTrixRichText.php index 6d8e1c2..3934016 100644 --- a/src/Traits/HasTrixRichText.php +++ b/src/Traits/HasTrixRichText.php @@ -64,4 +64,9 @@ public function trixAttachments() { return $this->morphMany(TrixAttachment::class, 'attachable'); } + + public function trixRender($field) + { + return $this->trixRichText->where('field', $field)->first()->content; + } } diff --git a/tests/Feature/HasTrixRichTextTest.php b/tests/Feature/HasTrixRichTextTest.php index dd1deb0..3a4e438 100644 --- a/tests/Feature/HasTrixRichTextTest.php +++ b/tests/Feature/HasTrixRichTextTest.php @@ -58,4 +58,16 @@ public function it_can_store_attachment() $this->assertFalse((bool) TrixAttachment::first()->is_pending); } + + /** @test */ + public function it_renders_the_content() + { + $post = Post::create([ + 'post-trixFields' => [ + 'content' => $expected = '

foo

', + ], + ]); + + $this->assertEquals($expected, $post->trixRender('content')); + } }