简体   繁体   English

如何将变量从控制器传递到视图并在 Laravel 的 Vue.js 上使用它?

[英]How to Pass Variable(s) from Controller to View and Use it on Vue.js in Laravel?

This is my second question in Stack Overflow.这是我在 Stack Overflow 中的第二个问题。 As mentioned in the previous one, I'm working on my project which allow users to upload many images at a time.正如前文所述,我正在开发我的项目,该项目允许用户一次上传许多图像。 Each image will be showed as a set of thumpnails.每个图像将显示为一组缩略图。 Before uploading, users can delete image(s) he/she doesn't want.在上传之前,用户可以删除他/她不想要的图像。 Finally, all the images selected and previewed will be stored using data from Vue.js.最后,所有选择和预览的图像都将使用来自 Vue.js 的数据进行存储。 Thank you @tamrat for his great guidance for that.感谢@tamrat 在这方面的出色指导。

This time, I want to create an edit page.这一次,我想创建一个编辑页面。 It will show the current value of each 'Repair Ticket' such as 'request subject', 'request details', and its 'photos'.它将显示每个“维修单”的当前值,例如“请求主题”、“请求详细信息”及其“照片”。 As always, you can edit those data and update the latest data.与往常一样,您可以编辑这些数据并更新最新数据。 I decided to use Vue.js which allow users to store many images.我决定使用 Vue.js,它允许用户存储许多图像。 However, I'm very new about using Vue.js in Laravel.但是,我对在 Laravel 中使用 Vue.js 很陌生。

For now, I'm trying to pass a variable which is '$repair_ticket' from the Controller to view which I set an edit form using Vue.js.现在,我正在尝试从控制器传递一个变量“$repair_ticket”,以查看我使用 Vue.js 设置的编辑表单。 I want to show the current data of this '$repair_ticket' which are '$repair_ticket->request_subject', '$repair_ticket->request_details', and 'repair_ticket->photos'.我想显示此“$repair_ticket”的当前数据,即“$repair_ticket->request_subject”、“$repair_ticket->request_details”和“repair_ticket->photos”。

For the first two value;对于前两个值; which are '$repair_ticket->request_subject' and '$repair_ticket->request_details', I'm trying to use 'v-bind:value="@{{$repair_ticket->request_subject}}"' and 'v-bind:value="@{{$repair_ticket->request_details}}"' in the input and textarea corresponsingly.这是'$repair_ticket->request_subject'和'$repair_ticket->request_details',我正在尝试使用'v-bind:value="@{{$repair_ticket->request_subject}}"'和'v-bind: value="@{{$repair_ticket->request_details}}"' 在 input 和 textarea 中相应。 However, it seems there's something wrong.不过,好像有什么地方不对。 Now, I can't show the current data of these two.现在,我无法显示这两个的当前数据。

For the last value which is the path file of photos 'repair_ticket->photos', I'm trying to display the current set of photos, but I don't know the way to pass the variable '$repair_ticket' and display it in View using Vue.js对于照片“repair_ticket->photos”的路径文件的最后一个值,我试图显示当前的照片集,但我不知道如何传递变量“$repair_ticket”并将其显示在使用 Vue.js 查看

Here is some part of the code in my Vue.js :这是我的 Vue.js 中的部分代码:

<template>
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card-header">Edit Request Form</div>

            <div class="card-body">
                <form class="vue-form" @submit.prevent="submit">
                    <fieldset>
                        <div class="form-group row rounded">
                            <label class="col-md-4 col-form-label text-md-right" for="request_subject">Request Subject<span style="color: red;">*</span></label>
                            <input class="col-md-6 form-control" type="text" name="request_subject" id="request_subject" required v-model="request_subject">
                        </div>

                        <div class="form-group row rounded">
                            <label class="col-md-4 col-form-label text-md-right" for="request_details">Request Details</label>
                            <textarea class="col-md-6 form-control" name="request_details" id="request_details" v-model="request_details" placeholder="Please leave your Request Details Here"></textarea>
                        </div>

                        <div class="form-group row">
                            <label class="col-md-4 col-form-label text-md-right" for="photos">Photo</label>
                            <input class="col-md-6 form-control" ref="photoUploadInput" type="file" multiple @change="handleFileSelect" style="display: none;">
                            <div class="col-md-6">
                                <div class="flex justify-between items-center mb-6">
                                    <div class="leading-tight">
                                        <div v-if="photos.length > 0">
                                            <p>You have selected {{ photos.length }} photo(s)</p>
                                        </div>
                                    </div>
                                    <button @click="clickInput" type="button" class="px-6 py-2 font-semibold rounded">Please Choose Your Photo(s)</button>
                                </div>
                                <div v-if="photos.length" class="-my-2 -mx-2 flex">
                                    <div v-for="(photo, index) in photos" :key="`thumb-${index}`">
                                        <div class="p-1">
                                            <div class="relative d-flex">
                                                <div class="inset-0" style="vertical-align: middle;">
                                                    <img class="object-cover" style="width: 50%;" :src="photo.preview" alt="Your Selected Photo">
                                                    <button @click="removePhoto(index)" type="button" class="rounded">
                                                        <i class="fas fa-minus" style="color: red;"></i>
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="form-group row mb-0 justify-content-center">
                            <button @click="upload" type="button" :disabled="!request_subject.length" :class="!request_subject.length ? 'cursor-not-allowed bg-gray-600 hover:bg-gray-600' : 'bg-indigo-500 hover:bg-indigo-600'" class="px-6 py-2 font-semibold rounded">Edit Request Form</button>
                        </div>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            request_subject: "",
            request_details: "",
            photos: []
        }
    },
    methods: {
        handleFileSelect(e) {
            Array.from(e.target.files).forEach(file => {
                const reader = new FileReader()
                reader.onload = () => {
                    this.photos.push({
                        preview: reader.result,
                        file
                    })
                }
                reader.readAsDataURL(file)
            })
        },
        clickInput() {
            this.$refs.photoUploadInput.click()
        },
        upload() {
            const dt = new DataTransfer()
            this.photos.forEach(photo => dt.items.add(photo.file))
            const fd = new FormData()

            fd.append('request_subject', document.getElementById('request_subject').value);
            fd.append('request_details', document.getElementById('request_details').value);

            this.photos.forEach((photo, index) => fd.append(`photo-${index}`, photo.file))
            axios.post('upload', fd, {
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
                // .then(response => console.log(response))
                .then(response => { 
                    console.log(response); 
                    window.location = 'view-all'; 
                })
                .catch(err => console.log(err))

        },
        removePhoto(index) {
            this.photos.splice(index, 1);
        }
    }
}
</script>

I'm not sure that this is the right way to do it or not.我不确定这是否是正确的做法。 If anyone has any suggestion, please leave your answers below.如果有人有任何建议,请在下面留下您的答案。

UPDATE : I have updated my code in Vue.js as below:更新:我在 Vue.js 中更新了我的代码,如下所示:

<template>
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card-header">Edit Request Form</div>

            <div class="card-body">
                <form class="vue-form" @submit.prevent="submit">
                    <fieldset>
                        <div class="form-group row rounded">
                            <label class="col-md-4 col-form-label text-md-right" for="request_subject">ชื่อเรื่อง<span style="color: red;">*</span></label>
                            <input class="col-md-6 form-control" type="text" name="request_subject" id="request_subject" required :value="repair_ticket['request_subject']">
                        </div>

                        <div class="form-group row rounded">
                            <label class="col-md-4 col-form-label text-md-right" for="request_details">รายละเอียด</label>
                            <textarea class="col-md-6 form-control" name="request_details" id="request_details" :value="repair_ticket['request_details']"></textarea>
                        </div>

                        <div class="form-group row">
                            <label class="col-md-4 col-form-label text-md-right" for="photos">Photos</label>
                            <input class="col-md-6 form-control" ref="photoUploadInput" type="file" multiple @change="handleFileSelect" style="display: none;">
                            <div class="col-md-6">
                                <div class="flex justify-between items-center mb-6">
                                    <div class="leading-tight">
                                        <div v-if="photos.length > 0">
                                            <p>You have selected {{ photos.length }} photo(s)</p>
                                        </div>
                                    </div>
                                    <button @click="clickInput" type="button" class="px-6 py-2 font-semibold rounded">Please choose photo(s)</button>
                                </div>
                                <div v-if="photos.length" class="-my-2 -mx-2 flex">
                                    <div v-for="(photo, index) in photos" :key="`thumb-${index}`">
                                        <div class="p-1">
                                            <div class="relative d-flex">
                                                <div class="inset-0" style="vertical-align: middle;">
                                                    <img class="object-cover" style="width: 50%;" :src="photo.preview" alt="Selected Photo">
                                                    <button @click="removePhoto(index)" type="button" class="rounded">
                                                        <i class="fas fa-minus" style="color: red;"></i>
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="form-group row mb-0 justify-content-center">
                            <button @click="upload" type="button" class="px-6 py-2 font-semibold rounded">Edit Request Form</button>
                        </div>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    props: ['repair_ticket'],
    mounted() {
        console.log(this.repair_ticket)
    },
    data() {
        return {
            repair_ticket_id: this.repair_ticket.id,
            request_subject: "",
            request_details: "",
            photos: []
        }
    },
    methods: {
        handleFileSelect(e) {
            Array.from(e.target.files).forEach(file => {
                const reader = new FileReader()
                reader.onload = () => {
                    this.photos.push({
                        preview: reader.result,
                        file
                    })
                }
                reader.readAsDataURL(file)
            })
        },
        clickInput() {
            this.$refs.photoUploadInput.click()
        },
        upload() {
            const dt = new DataTransfer()
            this.photos.forEach(photo => dt.items.add(photo.file))
            const fd = new FormData()

            fd.append('repair_ticket_id', this.repair_ticket_id);
            fd.append('request_subject', document.getElementById('request_subject').value);
            fd.append('request_details', document.getElementById('request_details').value);

            this.photos.forEach((photo, index) => fd.append(`photo-${index}`, photo.file))

            axios.put('re-upload/' + this.repair_ticket.id, fd, {
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
                // .then(response => console.log(response))
                .then(response => { 
                    console.log(response); 
                    window.location = 'view-all'; 
                })
                .catch(err => console.log(err))

        },
        removePhoto(index) {
            this.photos.splice(index, 1);
        }
    }
}
</script>

I have set route for this like below:我已经为此设置了如下路线:

Route::put('irepair/edit-form/re-upload/{id}', 'iRepair\RequestFormsController@update');

For now, I can show the current data of 'request_subject' and 'request_details', but still cannot show the current photos of this repair ticket.目前,我可以显示'request_subject'和'request_details'的当前数据,但仍然无法显示该维修票的当前照片。 Also, I still stuck on sending data to the controller and update the data the the database according to what changed.此外,我仍然坚持将数据发送到控制器并根据更改的内容更新数据库中的数据。 It keep saying Error 405.它一直说错误405。

Anyone has any suggestions for this?有人对此有什么建议吗? Please leave your answers below.请在下面留下您的答案。

You can use Vue props.您可以使用 Vue 道具。 hope this help you希望这对你有帮助

inside laravel blade内部 Laravel 刀片

<edit-ticket :repair-ticket="{{ json_encode($repair_ticket) }}"></edit-ticket>

Vue Component组件

export default {
     props:['repairTicket'],

     mounted() {
        console.log(this.repairTicket)
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM