Skip to content

Commit

Permalink
Add method to show field (#79)
Browse files Browse the repository at this point in the history
* add method to render the field's content

* add new method to docs
  • Loading branch information
diogogomeswww authored Mar 13, 2024
1 parent b0834d7 commit db4e2d4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<!-- inside view blade file -->

{!! $post->trixRender('content') !!} //must use HasTrixRichText trait in order for $model->trixRender() method work
```

### Storing Attachment

Expand Down
5 changes: 5 additions & 0 deletions src/Traits/HasTrixRichText.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions tests/Feature/HasTrixRichTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<h1>foo</h1>',
],
]);

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

0 comments on commit db4e2d4

Please sign in to comment.