获取表多重关系



我有以下雄辩的呼叫:

AppModelsAwardRecommendation::with('entities', 'countries')->get();

当我要求使用' countries'时,是因为' entities'表具有 country_id,而不是' awards'表。

我拥有的型号是:AwardRecommendationEntityErnySansLaraworldModelsCountries

我要做的是在查询中获取国家数据,但是在返回响应中,我才得到:

0 => array:27 [▼
"id" => 1
"guid" => "268f1080-67f3-48e5-844d-96b8c09c339b"
"user_id" => null
"title" => "Example Title"
"slug" => "example-title"
"intro" => null
"description" => null
"type" => "award"
"scope" => "firm"
"scope_name" => "1"
"scope_practice_area_id" => 1
"entity_id" => 1
"year" => null
"image_badge" => null
"image_one" => null
"image_two" => null
"notes" => null
"language" => "en"
"state" => 1
"ordering" => 0
"created_by" => null
"updated_by" => null
"deleted_by" => null
"created_at" => null
"updated_at" => null
"entities" => array:1 [▼
  0 => array:45 [▼
    "id" => 1
    "guid" => "b00e2be5-e1bb-4f98-8a35-30f2f7dfceba"
    "award_id" => 1
    "entity_name" => "Example entity"
    "slug" => "example-entity"
    "url" => null
    "description" => null
    "title" => "Mr."
    "first_name" => "John"
    "last_name" => "McNamarca"
    "email" => "john.mcnamara@examplecompany.com"
    "department" => null
    "phone" => "+1 (646) 412 123456"
    "mobile_phone" => null
    "home_phone" => null
    "other_phone" => null
    "fax" => null
    "assistant" => null
    "assistant_phone" => null
    "do_not_call" => 0
    "do_not_email" => 0
    "do_not_fax" => 0
    "email_opt_out" => 0
    "fax_opt_out" => 0
    "street" => null
    "city" => null
    "country_state" => null
    "zip" => null
    "country_id" => 240
    "street_other" => null
    "city_other" => null
    "state_other" => null
    "zip_other" => null
    "country_id_other" => 240
    "notes" => null
    "language" => "en"
    "state" => 1
    "ordering" => 0
    "created_by" => null
    "updated_by" => null
    "deleted_by" => null
    "deleted_at" => null
    "created_at" => null
    "updated_at" => null
    "pivot" => array:1 [▼
      "entity_id" => 1
    ]
  ]
]
"countries" => null

我该如何用Laravel雄辩完成此操作?

事先感谢您的任何帮助

首先需要定义Entity模型中的关系:

public function country()
{
    return $this->belongsTo(ErnySansLaraworldModelsCountries::class);
}

并确保ErnySansLaraworldModelsCountriesCountries模型的正确完整班级名称。

然后与相关实体和每个实体的国家一起加载奖励建议:

AwardRecommendation::with('entities.country')->get();

相关内容

  • 没有找到相关文章

最新更新