首页 > 解决方案 > 调用未定义的方法 Person::id()

问题描述

发生了一些非常奇怪的事情,当我没有在我的表单中上传任何图像时它不会抛出任何错误,但是当我这样做时会抛出这个:Call to undefined method App\Models\User_Management\Person::id()

前端代码

<template>
<div>
    <div class="container-2 mx-8 mt-10">
        <v-row>
            <v-col xs="12" sm="12" md="8">
                <v-card>
                    <v-form enctype="multipart/form-data">

                        <div class="container-2 mx-2">
                            <!-- Personal information -->
                            <v-divider></v-divider>
                            <p class="mt-2">Personal information</p>
                            <v-row>
                                <v-col cols="12" sm="12" md="4">
                                    <v-text-field v-model="person.first_name" counter="150" :rules="id="first_name" name="first_name" label="First Name" color="black"></v-text-field>
                                </v-col>

                                <v-col cols="12" sm="12" md="4">
                                    <v-text-field label="Last Name" v-model="person.last_name" counter="150" name="last_name" color="black"></v-text-field>
                                </v-col>

                            </v-row>

                            <v-divider></v-divider>
                            <p class="mt-2">Profile Picture</p>
                            <v-row>
                                <v-col cols="12" sm="12" md="6">
                                    <input type="file" id="avatar" ref="avatar" v-on:change="fileUpload()"/>

                                </v-col>
                            </v-row>

                            <div class="right-align">
                                <v-btn color="yellow" class="black-text" @click="update()">Submit</v-btn>
                                <v-btn color="red accent-3" class="black-text">Back</v-btn>
                            </div>
                            <br>
                        </div>
                    </v-form>
                </v-card>
            </v-col>
        </v-row>
    </div>
</div>
</template>

<script>

export default {
    data() {
        return {
            //Data
            person: {
                first_name: '',
                last_name: '',
            },
            userAvatar: undefined,
            person_id: 1,

            }

        }
    },


    methods: {
        fileUpload(){
            this.userAvatar = this.$refs.avatar.files[0];
        },

        update() {
            let formData = new FormData();
            formData.append('first_name', this.person.first_name);
            formData.append('last_name', this.person.last_name);
            formData.append('profile_picture', this.userAvatar);
            formData.append('_method', 'PUT');

            axios.post(`/profile/${this.person_id}`, formData, {
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
                .then(res => {
                    console.log(res.data);
                }).catch(e => {
                    console.log(e);
                })

        },

        }

    }
}
</script>

后端代码

public function update(Request $request, $id)
    {
        //Person
        $person = Person::find($id);

        //Avatar
        $name = null;
        $image = null;

        if ($request->hasFile('profile_picture')){
            $file = $request->file('image');
            $name = $person->id();
            $file->move(public_path().'/img/uploads/avatars', $name);

            //Image
            $image = new Image();
            $image->path = $name;
            $image->save();
        }

        //Profile
        $person->first_name = $request->first_name;
        $person->last_name = $request->last_name;
        if ($image)
            $person->image_id = $image->id;
        $person->save();
    }

我对模型的导入是这样写的 use App\Models\User_Management\Person;

出于某种原因,当我上传图片时,它似乎无法识别 ::find($id) 方法,但我不知道为什么,有什么解决方法吗?我试图记录 $request 变量并抛出以下内容

[2020-04-13 19:41:23] local.INFO: array (
  'first_name' => 'John',
  'last_name' => 'Smith',
  '_method' => 'PUT',
  'profile_picture' => 
  Illuminate\Http\UploadedFile::__set_state(array(
     'test' => false,
     'originalName' => 'kekas.jpg',
     'mimeType' => 'image/jpeg',
     'error' => 0,
     'hashName' => NULL,
  )),
)  

前端代码似乎没有问题,但我把它留在这里,以防它提示这个奇怪的错误。

标签: laravelvue.js

解决方案


Person Instanse 中没有方法$person->id();。你需要得到这样的属性:$person->id. 希望能帮助到你。您在此代码行中出错:$name = $person->id();


推荐阅读