1

I am passing an object from my controller to my blade.php.file. From there I use object notation to get the variables from my object which leaves me with an array. I then am trying to pass that array of data from my blade.php file to my vue component. I want to pass the entire array not just one index of it. As of right now when I try to pass the array I get the error htmlspecialchars() expects parameter 1 to be string, array given which makes sense because I am passing an array (which is what I want). How do I successfully pass an array of data from laravel blade to VUE?

blade.php file

<div id="app">
   <draggable
       draggable-table="{{ $table->vars }}">
    </draggable>
</div>

VUE component

<template>

</template>

<script>
    export default {
       props: ['draggableTable'],
   };

</script>

EDIT:

As suggested to me I added a : and json_encode in my blade fole table like so: :draggable-table="{{json_encode($table->vars)}}"> But when I check my VUE console my component no longer shows and only root is shown with none of the array data passed in. (See photo)

enter image description here

2ND EDIT:

I am trying to do this via axios now. I added this to my component within the <script> tags

 data(){
          return{
            table: this.draggableTable
          }
      },
       methods: {
          
          getData(){
              axios.get(this.endpoint, {
                 table: this.table,
              }).then(res => {
                 console.log(res);
              }).catch(err => {
                 console.log(err);
              });
          }
       },

computed: {
          endpoint(){
             return window.location.protocol + "//" + window.location.host + '/users';
          }
      }

app.js

const app = new Vue({
  el: '#app'
});

So now how to I pass the data from blade to VUE?

Thank you.

10
  • <draggable :draggable-table="@json($table->vars)"></draggable> should work. Commented Jul 22, 2020 at 15:44
  • @Remul I tried your solution as well but in my VUE browser console, only the root component appears with no data included. Commented Jul 22, 2020 at 16:09
  • can you share the app.js or where you crea vue instance please Commented Jul 22, 2020 at 19:49
  • @TEFO just posted it in my question Commented Jul 22, 2020 at 19:50
  • so you didnt register it locally in root instance. did you register this component globally? Commented Jul 22, 2020 at 19:52

2 Answers 2

1

you have to add : in prop, live below code.

<div id="app">
   <draggable
       :draggable-table="{{ json_encode($table->vars) }}">
    </draggable>
</div>
Sign up to request clarification or add additional context in comments.

5 Comments

I tried to implement your solution but it did not work for me. My data was not passed through to my component. I edited my question to include the details and a photo. Thank you.
I think there is a component load issue so could you please check why component is not loading.
What do you mean?
Your component is not loading.
So Using axios I was able to pass the data BUT it is coming through as a string of html, like this: “<div id=“users-table”><table>....table data here...</table></div>” and I am using the json_encode in my blade file. I want to pass the array not a string of HTML. How do I do this?
0

You can just send it as a JSON object and then parse it either in Vue or when accepting it as a prop.

<div id="app">
   <draggable
       :draggable-table=JSON.parse({{ json_encode($table) }})>
    </draggable>
</div>

Why would you need to pass data from Blade to Vue if you are using an API call in Vue? Data is already available to you.

7 Comments

I reverted my component to be simple and removed axios. It once again looks like it did in the original part of my question. So in my blade file I used your code snippet posted above to try to pass data from blade.php to VUE component but in my console I get this error: [Vue warn]: Error in render: "SyntaxError: Unexpected token u in JSON at position 0" (found in <Root>), is your syntax correct?
What is the server returning? Does $table exist? What do you get when you just echo {{ json_encode($table) }}?
I am in a blade file so I have to @dd() $table but when I dd($table) I get back ComponentView {#1538 ▼ +vars: array:38 [▶] +parent: null +children: [] -rendered: true } If I add the json_encode, then when I dd($table) it returns false.
Just {{ json_encode($table) }} in your view. What do you get? And do you have laravel/telescope?
So I get a long string containing HTML. The html is the table I am trying to pass to VUE but It is going through as a string of HTML. ""<div class='box' data-plugin='true'><div id='users_table' class='users-style'><div....etc"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.