0

I am beginner webdeveloper. I use in my project Laravel 8 and Vue.

I have this code:

<template>
    <div class="sm:bg-transparent pt-6 md:mt-16 w-full px-4 md:px-0">
        <h1 class="text-dark text-4xl sm:text-3xl font-medium sm:text-center mb-6">{{ __('Archiwum') }}</h1>


        <preloader
            v-if="isLoadingInProgress"
            class="py-8"
            :class="preloaderClass"
        />

        <div class="text-center" v-if="gasmeters.length == 0 && !isLoadingInProgress">
            {{ __('Nie posiadasz jeszcze żadnych odczytów') }}
        </div>

        <div v-if="gasmeters.length">
            <div class="bg-white rounded border border-stroke flex items-start p-6"
                 v-for="(gasmeter, index) in gasmeters"
                 v-bind:key="`gasmeter-${index}`">
                <div class="mr-6">
                    <img class="mb-2" :src="gasmeters.img"/> {{ gasmeters }}
                </div>
                <div class="w-1/5">
                    <span class="font-bold text-xl text-grey">Lipiec 2020 (#123)</span>
                </div>
                <div class="w-1/5">
                    <span class="font-bold text-xl text-grey">Stan:</span> <span>2120</span>
                </div>
                <div class="w-1/5">
                    <span class="font-bold text-xl text-grey">Data:</span> <span>2020.01.12</span>
                </div>
                <div class="w-1/5">
                    <span class="text-xl">Zgłoszone przez aplikację</span>
                </div>
            </div>
        </div>


    </div>
</template>
<script>
import axios from "axios";
import Preloader from "@/common/components/Preloader";

export default {
    components: {
        Preloader
    },
    mounted() {
        this.loadGasmeters();
    },
    data() {
        return {
            gasmeters: [],
            isLoadingInProgress: false,
        }
    },
    computed: {
        preloaderClass() {
            return this.gasmeters.length > 0
                ? ""
                : "loading--transparent loading--block";
        }
    },
    methods: {
        loadGasmeters() {
            this.isLoadingInProgress = true;

            axios
                .get(route('api.archive.list'))
                .then(response => {
                    this.gasmeters = _.get(response, "data.data", []);
                    console.log(this.gasmeters);
                })
                .catch(error => {
                    console.log(error);
                })
                .then(() => {
                    this.isLoadingInProgress = false;
                });
        },

    }
}
</script>

My php return me:

{
    "data": [
        [
            {
                "id": 1,
                "value": "99.00",
                "settlement_date": "2020-10-20T22:00:00.000000Z",
                "loaded_from": "Stan pobrany z systemu BAŁTYKGAZ",
                "img": "img/icons/crm.svg",
                "created_at": "2020-10-21T15:09:22.000000Z"
            },
            {
                "id": 2,
                "value": "109.00",
                "settlement_date": "2020-10-20T22:00:00.000000Z",
                "loaded_from": "Zgłoszone przez aplikację",
                "img": "img/icons/cms.svg",
                "created_at": "2020-10-21T17:02:18.000000Z"
            }
        ]
    ]
}

I have problem with my part this code:

<div v-if="gasmeters.length">
            <div class="bg-white rounded border border-stroke flex items-start p-6"
                 v-for="(gasmeter, index) in gasmeters"
                 v-bind:key="`gasmeter-${index}`">
                <div class="mr-6">
                    <img class="mb-2" :src="gasmeters.img"/> {{ gasmeters }}
                </div>
                <div class="w-1/5">
                    <span class="font-bold text-xl text-grey">Lipiec 2020 (#123)</span>
                </div>
                <div class="w-1/5">
                    <span class="font-bold text-xl text-grey">Stan:</span> <span>2120</span>
                </div>
                <div class="w-1/5">
                    <span class="font-bold text-xl text-grey">Data:</span> <span>2020.01.12</span>
                </div>
                <div class="w-1/5">
                    <span class="text-xl">Zgłoszone przez aplikację</span>
                </div>
            </div>
        </div>

I want replace my img by img from json, "Lipiec 2020 (#123)" => gasmeters.settlement_date (# gasmeters.id), "Stan: " 2120 => Stan: gasmeters.value

How can I make it?

This: {{ gasmeters }} return me:

[ [ { "id": 1, "value": "99.00", "settlement_date": "2020-10-20T22:00:00.000000Z", "loaded_from": "Stan pobrany z systemu BAŁTYKGAZ", "img": "img/icons/crm.svg", "created_at": "2020-10-21T15:09:22.000000Z" }, { "id": 2, "value": "109.00", "settlement_date": "2020-10-20T22:00:00.000000Z", "loaded_from": "Zgłoszone przez aplikację", "img": "img/icons/cms.svg", "created_at": "2020-10-21T17:02:18.000000Z" } ] ]

Please help me

3
  • try <img class="mb-2" :src="gasmeter.img"/> {{ gasmeter.img }} Commented Oct 22, 2020 at 7:01
  • it's not working :( Commented Oct 22, 2020 at 7:14
  • then add code of php which return that json you need to fix that side Commented Oct 22, 2020 at 7:15

1 Answer 1

2

I saw Your problem. Because your response data from service API Laravel is the array in array. If you want to get your right data, you can do in 2 way:

  • You change the front end data when get from service API, like this:
    axios.get(route('api.archive.list'))
            .then(response => {
                this.gasmeters = _.get(response, "data.data[0]", []);
                console.log(this.gasmeters);
            })
            .catch(error => {
                console.log(error);
            })
            .then(() => {
                this.isLoadingInProgress = false;
            });

enter image description here

  • Or you can change the Laravel API like this:
    {
        "data": [{
                    "id": 1,
                    "value": "99.00",
                    "settlement_date": "2020-10-20T22:00:00.000000Z",
                    "loaded_from": "Stan pobrany z systemu BAŁTYKGAZ",
                    "img": "img/icons/crm.svg",
                    "created_at": "2020-10-21T15:09:22.000000Z"
                },
                {
                    "id": 2,
                    "value": "109.00",
                    "settlement_date": "2020-10-20T22:00:00.000000Z",
                    "loaded_from": "Zgłoszone przez aplikację",
                    "img": "img/icons/cms.svg",
                    "created_at": "2020-10-21T17:02:18.000000Z"
                }
            ]
    }

enter image description here

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

Comments

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.