2

I'm trying to build a page on Laravel 5.4 which contains few data which needs to be manipulated and then sent across the views. My view contains components of the vuejs v2.0. I want those data to be implemented in the components. I tried using the laracasts PHP Vars to JS transformer but unable to get it. I followed the steps by placing "laracasts/utilities": "~2.0" in my composer.json then I added the serviceprovider as mentioned in the documentation, I published the vendor and added the following in config/javascript.php,

'bind_js_vars_to_this_view' => 'Nitseditor.show',

I'm having a dynamic views folder which is currently inside my Nitseditor\home\resources\views Now in my controller I'm having following codes:

public function show()
{
    JavaScript::put([
        'foo' => 'bar',
        'age' => 29
    ]);
    return view(Nitseditor.show);
}

Now first of all it was throwing an error as I see that it was including use MongoDB\BSON\Javascript; then I removed and tried using use JavaScript

Now in the app.js file which is present in my asset folder, I'm including each components and trying to do console.log(foo); but its throwing an error foo not defined.

2 Answers 2

2

There are a few ways to do this depending on what you are trying to achieve and how your project is set up. The simplest way is to make a request to your controller from inside your component that returns json. Laravel 5.4 comes with axios so you can use that:

methods: {
  getData(){
    axios.get('/controller/route')
    .then(response => {
      // set your variables from the response
      this.myData = response.data;
    })
  .catch(error => {
    console.log(error);
  });
},
data(){
  return {
    myData: {}
  }
}

If you need child components to access the data then you would need to put that in the parent and pass myData" using props.

You could also create a directive and pass your variable down directly from your blade template:

Vue.directive('init', {
  bind: function(el, binding, vnode) {
    vnode.context[binding.arg] = binding.value;
  }
});

Then you would just need to do:

<div v-init:vars="{foo: 'foo', age: 29}"></div>

And pass vars as props to any component that needs them:

Here's the JSFiddle: https://jsfiddle.net/3e05qwLh/

If you have multiple descendants that rely on your variables you will probably want to look at using vuex.

Sign up to request clarification or add additional context in comments.

1 Comment

can you please explain me how to do this..stackoverflow.com/questions/43972797/…
-1

Dunno if it helps but I use this method for passing variables to javascript:

// in master layout (head section)
<meta name="foo" content="{{ $foo }}">

// in javascript (included or in the template)
foo = $('meta[name=foo]').attr('content');

If you have javascript in the blade template you can use directly this method:

foo = "{{ $foo }}";

2 Comments

You are using jQuery even though the author asked for plain javascript + vue. You should add the plain javascript syntax for accessing the meta data
Nobody ask for "plain" javascript, and laravel 5.4 default installation include jQuery, see there github.com/laravel/laravel/blob/master/resources/assets/js/… So your comment is not really appropriate

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.