【问题标题】:how to access the relationships via models in laravel 8 inertia如何通过 laravel 8 惯性中的模型访问关系
【发布时间】: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


【解决方案1】:

您需要手动加载所有要显示的关系。与 Blade 不同,您不能只访问与 $user-&gt;area 的关系,因为 $user 不是 Eloquent 实例,而是您作为 JSON 返回到 Vue 实例的内容。

从您的控制器调用$user-&gt;load('area')。这将使您可以使用area

【讨论】:

    【解决方案2】:

    我有同样的问题,但最后我发现了另一个技巧, 我在模型中定义了另一个方法并添加了一个属性

    在您的情况下: 尝试这个: 面积.php

    class Area extends Model
    { ....
       $appends = ['users'];
    
       public function users()
        {
            return $this->hasMany(User::class);
        }
        
       // define a methode getUsersAttribute()
       public function getUsersAttribute(){
          return $this->users()->get();
       }
      
    

    【讨论】:

    • 小心:使用此代码,每次加载单个/多个区域时都会加载所有区域的用户。没有任何分页或限制。这将是非常资源贪婪的。查看@Tim 的答案:您需要先加载与 Eloquent 的关系,然后才能将其作为 JSON 返回到视图/JS。
    猜你喜欢
    • 2014-05-15
    • 2023-03-21
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 2014-04-02
    • 2013-04-07
    • 1970-01-01
    相关资源
    最近更新 更多