Skip to content
Advertisement

Pass data to javascript but got so many backslash

I’m passing data from my controller to javascript.

public function index()
    {
        $events = Eventcalendar::get()->pluck('event');
        return view('eventcalendar.index',compact('events'));
    }

this is I do it in the blade

<script type="text/javascript">
  var data = {!! $events !!}

  console.log(data) //the result is in the screenshot
</script>

how can I remove the backslash in the JSON data?

Note: event column datatatype is JSON

I’m using LARAVEL 7

Thank you

enter image description here

Advertisement

Answer

Solved it by adding

protected $casts = [
        'event' => 'array'
    ];
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement