mina's avatar
Level 2

Laravel - Vue.js retrieve model best Practices

Hi All,

I am seeking your support on the best practices for API development. I am developing a new application on Spark and using Vue.js as my front-end framework. And I am seeking your support to advise with the best way to retrieve single model.

For example: Route::get('product/{product}', 'ProductController@show');

In order to check the Authorisation of this route, in all cases I will retrieve the model Product with the passed id on the controller responsible for handling the front-end requests.

However, should I pass the model directly to blade and display the information, or better to pass only the id to blade and hence to Vue.js, where it will make another call to the API to retrieve the model.

Appreciate your support.

Best Regards

Mina

0 likes
4 replies
leolam2005's avatar

I believe when you design an API which you and others consume in the same way, that can be considered good design.

Normally, I would use this approach:

Define RESTful end points like api/v1 /product/{product} then return $product->toJSON()

When I am landed on a view page, I use the user's API token / csrf_token to authenticate and fetch the resources.

My Vue gets the resources, so it is responsible to JSON.parse the data to view right after api call.

Just like other people how they will consume your API

note: you might not need toJSON() and Laravel / lumen will make it JSON for you. But in some cases after proxy, you will find that you might need it.

mina's avatar
Level 2

Thanks for your feedback. However, I see a little bit of load on the web server. As the server will fetch the same resource twice. The first time on the web part, where I will check the authorization for the resource, and make sure the user has access to that resource. The second time on the API to return it, and display its information.

What do you think?

WebKenth's avatar

When i design a website i typically serve a fresh view with the information it needs, and then use vue to interact with it and whenever the user makes a change ( like adding an item to a list) i send a request to the server for the updated information

An example with pseudo code (simplified a lot):

User visits /component -> Laravel serves the request and send through a component.blade.php User clicks delete, Vue sends it's delete request and sends a get request for updated data

component.blade.php

<component items="{{ $items }}"></component>

component.vue

<template>
    <button @click="add">Add to List</button>
    <ul>
        <li v-for="item in items">{{ item.name }} <button @click="delete(item)">delete</button></li>
    </ul>
</template>
<script>
export default
{
    props:{
        items: {}
    },
    methods:{
        add: function(){
            // send add request
            // this.refresh()
        },
        delete: function(item){
            // send delete request
            // this.refresh()
        },
        refresh: function(){
            // get request to fetch items
        }
    }
}

This way when the user loads the page first time, fresh data is sent in and the page can function as a single page application without refreshes

The API you create is what Vue uses to fetch data, the same routes your API clients would use and the Web route is what laravel will use to serve your basic route data.

The split here makes it so laravel functions both as an API service and you can keep your generic page logic seperated from your API logic

mina's avatar
Level 2

Hello @WebKenth, thanks a lot for your reply. However; your example does not cover the specific example of model Get part (show action in laravel rest terminology).

The show.blade.php should be served with the whole model properties, or preferably, just the model id and leave the remaining of the attributes to be served by Vue.js?

Please or to participate in this conversation.