0

Ran into a bit of a problem with combining Laravel and Vue.js to populate a table.

Essentially, I was trying to use the v-repeat property in combination with a http:get request using the vue-resources extension. The problem is that no values appear to be getting passed through by Vue - I simply get the {{first_name}} and {{email_address}} in brackets.

I can confirm that the API method that is called by the http:get request is in fact spitting out data (manually accessing the URL in the browser reveals data).

Here is the code in the routes.php file that is responsible for outputting the data:

get('api/v1_users',function()
{
    return App\User::all()->toJson();
});

And here is what it spits out in the browser:

[{"email_address":"[email protected],"first_name":"John","password":"test123"}]

The Chrome console displays no errors nor warnings.

Here is my blade file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>Navbar Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link media="all" type="text/css" rel="stylesheet" href="{{ URL::asset('css/bootstrap.min.css') }}">

    <!-- Custom styles for this template -->
    {!! Html::style('css/navbar.css') !!}


</head>

<body>

<div class="container">

    <!-- Static navbar -->
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">User Password Operations</a>
            </div>
            <div id="navbar" class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li class="inactive"><a href="#">Reset New User</a></li>
                    <li class="inactive"><a href="#">Pending Users</a></li>
                </ul>
            </div><!--/.nav-collapse -->
        </div><!--/.container-fluid -->
    </nav>

    <!-- Main component for a primary marketing message or call to action -->
    <div class="jumbotron">
        <h1>Pending 1.0 Users</h1>
        <p>A list of 1.0 users that have a change!me password as a result of this tool, and are awaiting password change.</p>
    </div>
    <table class="table table-bordered">
        <tr>
            <td>
                <b>Name</b>
            </td>
            <td>
                <b>Email</b>
            </td>
            <td>
                <b>Select</b>
            </td>
        </tr>
        <div id = "user">
            <tr v-repeat = "user: v1_user">
                <td>
                    @{{ first_name }}
                </td>
                <td>
                    @{{ email_address }}
                </td>
                <td>
                    <button type="button" class="btn btn-success">Revert Password To Original</button>
                </td>
            </tr>
        </div>
    </table>
    <div class="jumbotron">
        <h1>Pending 2.0 Users</h1>
        <p>A list of 2.0 users that have a change!me password as a result of this tool, and are awaiting password change.</p>
    </div>
    <table class="table table-bordered">
        <tr>
            <td>
                <b>Name</b>
            </td>
            <td>
                <b>Email</b>
            </td>
            <td>
                <b>Select</b>
            </td>
        </tr>
        <div>
            <tr v-repeat = "user: v1_user">
                <td>
                    @{{user.first_name}}
                </td>
                <td>
                    @{{user.email_address}}
                </td>
                <td>
                    <button type="button" class="btn btn-success" v-on= "click: onClick">Revert Password To Original</button>
                </td>
            </tr>
        </div>
    </table>
</div> <!-- /container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Vue.js file REP  -->
<script src="/js/vue.js"></script>
<script src="/js/vue-resource.min.js"></script>
<!-- Main Vue file-->
<script src="/js/main.js"></script>

</body>
</html>

Here is the accompanying javascript file with the Vue.js code: (main.js)

  new Vue({
    el: "#user",

    data:
    {
        v1_user:[],
    },

    ready : function()
    {
        this.fetchV1IntermediaryUsers();
    },

    methods:
    {
        fetchV1IntermediaryUsers: function() {
            this.$http.get('/api/v1_users',function(v1users) {
                this.$set('v1_user',v1users);
            });

        }
    }
});
8
  • Check your browser console and post if there are any errors.
    – chanafdo
    Commented Jul 21, 2015 at 7:01
  • Added a screenshot above! Commented Jul 21, 2015 at 13:50
  • 1
    You have two different sections with the same id user. One section won't be affected by Vue. Try different instances with different ids or other method. Are you getting one section correctly or neither one.
    – chanafdo
    Commented Jul 21, 2015 at 14:06
  • Hmm good to know. The problem is that neither section is properly coming up... I've tried changing id's and whatnot, but it doesn't seem to help. It looks like vue is running based off that warning, so I'm completely out of ideas as to why it isn't working Commented Jul 21, 2015 at 14:13
  • Though, my last idea would be that it is because the element I'm referencing is the parent of v-repeat element (v1_user), but the actual data is being spit out under the <td> tags. Would this matter? Can the children of the v-repeat element access the data without any special tags or modifiers? Commented Jul 21, 2015 at 14:14

1 Answer 1

3

You have multiple DIV's with the same ID's. ID's in HTML must be unique. When you start a VUE instance you bind it to an element, in this case which is in your code twice. Remove the ID's and add an ID to the <Body> tag, then check your code.

1
  • Thanks a ton! This was causing me so much frustration! Commented Jul 21, 2015 at 22:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.