Home > Blockchain >  javascript syntax error while get data (object) from laravel controller
javascript syntax error while get data (object) from laravel controller

Time:06-09

get javascript syntax error in .blade.php for get $data->modal

my controller function :

    public function buku_all($page, $modal)
    {$data = (object) [
            'sidebar' => "pelayanan",
            'breadcrumbsub' => 'Data Buku',
            'modal' => $modal
            'page' => $page
            ];
    return view('admin.buku')->with('data', $data);
    }

my js in admin/buku.blade.php

if ($data->modal == 'book') {
        $(window).on('load', function() {
            $('#modal_edit_1').modal('show');
        });
    }

CodePudding user response:

One cannot mix PHP and JS syntax.

@isset ($data)
   <script>
       let data = {{ json_encode($data) }};
       ...
   </script>
@endisset

Usually it's more alike this.

CodePudding user response:

Try this.

@isset ($data)
   <script>
       var data = "{{ json_encode($data) }}";
       console.log(data);
       ...
   </script>
@endisset
  • Related