从Laravel / Laravel Nova中的另一个模型返回数据

问题描述 投票:0回答:1

我的问题:

在Laravel Nova中,如何根据国家ID?

我有2张桌子:

  1. 国家
  2. 国家/地区价格

  1. 国家表保存id, name等。
  2. countries_price表保留id, country_id, country_price

模型: ((Laravel和Nova)] >>

在国家模型中(Laravel):

    public function countryPrice() {
        return $this->hasOne('App\\CountryPrice');
    }

在CountryPrice模型(Laravel):

    public function countries()
    {
        return $this->hasMany('App\Country', 'id', 'country_id');
    }

在国家/地区模型(Nova):

HasOne::make('CountryPrice'),

在CountryPrice模型中(Nova):

HasMany::make('Countries'),

我正在尝试:

在CountryPrice模型中(Laravel)

    public function getCountryName() {
        return Country::where('id', $this->country_id)->first()->name;
    }

在CountryPrice模型中(Nova)

    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            ID::make('Country ID','country_id'),

// TRYING TO PULL IN COUNTRY NAME HERE
            Text::make('Country Name', function () {
                $countryName= $this->getCountryName;
                return $countryName;
            }),

            Text::make('Base Price', 'trip_cost'),
            HasMany::make('Countries'),
        ];
    }


我的错误:

App \ CountryPrice :: getCountryName必须返回一个关系实例


我不了解该错误,需要帮助才能使其正常工作。任何帮助,将不胜感激。

我的问题:在Laravel Nova中,如何显示基于国家/地区ID的国家/地区名称?我有2个表:countries country_price国家表包含id,名称等。country_price ...

php laravel relationship laravel-nova
1个回答
0
投票

修正关系

© www.soinside.com 2019 - 2024. All rights reserved.