Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

So i have made an api using laravel 5.2, and used VueJs to pull data from it. I was able to fetch data for the index() action by fetching all the results. Now my issue becomes little more complicated. I need to make a show() method so that each post from the table would go to it's single post page.

methods: {
        fetchItems: function (page) {
            var data = {page: page};
            this.$http.get('api/v1/pages', data).then(function (response) {
                //look into the routes file and format your response
                this.$set('items', response.data.data);
                this.$set('pagination', response.data.pagination);
            }, function (error) {

            });
        },
        changePage: function (page) {
            this.pagination.current_page = page;
            this.fetchItems(page);
        }
    }

This is my VueJs Code.

 <div class="col-md-12" v-for="post in items" track-by="$index">
            <a href="@{{ post.slug }}"><h1>@{{ post.title }}</h1></a>
            <p>@{{ post.body }}</p>
 </div>

This is my Show all posts

My questions: 1. Should i create a separate file for show method ? 2. How can i leverage laravel ioc to get $slug from post model and show single page.

share|improve this question
up vote 0 down vote accepted

You don't have to create a new page, however, unless you use something like Vueify, to write single file components, you may want to. It will be easier.

The same way you created your list view, ajax call and index api, you can create a view, ajax call and api, to show our posts (using post ids). Your api response should send all the data you need to display, including the slug.

Based on your github repository, create a route like this for your api, where id is the post id:

Route::resource('show/{id}', 'PagesController@show');
share|improve this answer
    
I tried working with it, and having trouble understanding how to write the ajax call for it. Can you help me ? – manshu Jun 6 at 23:38
    
Sure. The ajax call you should be able to write very easy, I can tell you that, since you already your fetch items one. Are you able to create the api? Or do you have questions about it? – crabbly Jun 6 at 23:59
    
I've uploaded the code to github. github.com/manshu/LaravelNoPassword – manshu Jun 7 at 0:02
    
Looking at your github repository, I see you already have a method to show the post, create the route I just updated the answer with, then test it. After, create an ajax all from a Post view page to access it. – crabbly Jun 7 at 0:28
    
I've tried that already, it doesn't work. – manshu Jun 7 at 0:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.