【发布时间】:2021-06-03 13:21:03
【问题描述】:
我在用户表和区域表之间有一对多的关系,当我返回配置文件数据时,我从用户表中获取 area_id,我需要使用模型获取区域名称。 有没有办法在配置文件视图中获取区域名称? 我尝试在 show.vue 中调用模型函数,但它不起作用。
用户.php
public function area()
{
return $this->belongsTo(Area::class);
}
区域.php
public function users()
{
return $this->hasMany(User::class);
}
show.vue
<template>
<app-layout>
<template #header>
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
Profile
</h2>
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
Area :
</h2>
</template>
<div>
<div class="max-w-7xl mx-auto py-10 sm:px-6 lg:px-8">
<div v-if="$page.props.jetstream.canUpdateProfileInformation">
<update-profile-information-form :user="$page.props.user" />
<jet-section-border />
</div>
<div v-if="$page.props.jetstream.canUpdatePassword">
<update-password-form class="mt-10 sm:mt-0" />
<jet-section-border />
</div>
<div v-if="$page.props.jetstream.canManageTwoFactorAuthentication">
<two-factor-authentication-form class="mt-10 sm:mt-0" />
<jet-section-border />
</div>
<logout-other-browser-sessions-form :sessions="sessions" class="mt-10 sm:mt-0" />
<template v-if="$page.props.jetstream.hasAccountDeletionFeatures">
<jet-section-border />
<delete-user-form class="mt-10 sm:mt-0" />
</template>
</div>
</div>
</app-layout>
</template>
<script>
import AppLayout from '@/Layouts/AppLayout'
import DeleteUserForm from './DeleteUserForm'
import JetSectionBorder from '@/Jetstream/SectionBorder'
import LogoutOtherBrowserSessionsForm from './LogoutOtherBrowserSessionsForm'
import TwoFactorAuthenticationForm from './TwoFactorAuthenticationForm'
import UpdatePasswordForm from './UpdatePasswordForm'
import UpdateProfileInformationForm from './UpdateProfileInformationForm'
export default {
props: ['sessions'],
components: {
AppLayout,
DeleteUserForm,
JetSectionBorder,
LogoutOtherBrowserSessionsForm,
TwoFactorAuthenticationForm,
UpdatePasswordForm,
UpdateProfileInformationForm,
},
}
</script>
【问题讨论】:
-
请分享您的架构。和控制器代码。
-
主要是人们尝试像这样访问模型函数 $user->area();这是完全错误的。正确的方法是 $user->area;没有花括号。
-
我通过 vue 组件获取用户对象,所以我不能使用“$user->area”
-
尝试使用“user.area”。 “user.name”可以正常工作吗?
-
user.area 不打印任何东西 ): ,user.name 打印用户名。这是用户 obj 的示例:{“id”:2,“fname”:“”,“lname”:“”,“email”:“test@test.com”,“email_verified_at”:“2021-02-22T22 :44:50.000000Z”,“current_team_id”:2,“area_id”:2,“profile_photo_path”:空,“created_at”:“2021-02-22T22:36:14.000000Z”,“updated_at”:“2021-03 -04T21:02:38.000000Z", "profile_photo_url": "ui-avatars.com/api/…", "two_factor_enabled": false }
标签: vuejs2 laravel-8 inertiajs