javascriptphplaravelalpine.js

How to escape properly a text in alpinejs/Laravel?


I am using alpinejs and laravel, I stored in the database this text: F & M, this part of my blade file:

.....
<input   type="text" name="title"   id="title"  x-model="title" required />
 ....

<script> 
document.addEventListener('alpine:init', () => {
      Alpine.data('MyForm', () => ({ 
              title: '{{ $post->title }}',
      })) 
 }) 
    </script> 

In the input text the title is displayed like this:

F &amp; M

I want it to be displayed like this: F & M . I don't want to use {!! !!} because it may be unsafe for my website, What can I do?


Solution

  • Try this :

    <script> 
    document.addEventListener('alpine:init', () => {
        Alpine.data('MyForm', () => ({ 
            title: @json($post->title),
        })) 
     }) 
    </script>