programing

8단계 관성의 모델을 통해 관계에 접근하는 방법

goodcopy 2022. 7. 29. 23:16
반응형

8단계 관성의 모델을 통해 관계에 접근하는 방법

나는 사용자 테이블과 영역 테이블 사이에 1대 다의 관계가 있는데, 프로파일 데이터를 반환할 때 사용자 테이블에서 area_id를 얻을 때 모델을 사용하여 영역 이름을 얻어야 합니다.프로파일 뷰에서 영역 이름을 얻을 수 있는 방법이 있나요? 쇼에서 모델 함수를 호출하려고 했습니다.vue는 동작하지 않습니다.

User.php

 public function area()
    {
        return $this->belongsTo(Area::class);
    }

Area.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는 웅변 인스턴스가 아니라 Vue 인스턴스에 JSON으로 반환하는 인스턴스입니다.

에서 " "를 호출합니다.$user->load('area') ★★★★★★★★★★★★★★★★★★★ ★area이용하실 수 있습니다.

나도 같은 문제가 있었지만, 마침내 다른 트릭을 발견했고, 내 모델에서 다른 메서드를 정의하고 속성을 추가했다.

고객님의 경우:이것을 시험해 보세요.Area.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();
   }
  

언급URL : https://stackoverflow.com/questions/66482756/how-to-access-the-relationships-via-models-in-laravel-8-inertia

반응형