获得table个关系

Get table multiple relations

我有以下 eloquent 电话:

\App\Models\AwardRecommendation::with('entities', 'countries')->get();

当我用'countries'请求时是因为'entities'table有country_id而不是'awards'table.

我有的型号是:AwardRecommendationEntityErnySans\Laraworld\Models\Countries

我想做的是在查询中获取国家/地区数据,但在 return 响应中我只得到:

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 Eloquent 完成此操作?

在此先感谢您的帮助

您需要先在Entity模型中定义关系:

public function country()
{
    return $this->belongsTo(ErnySans\Laraworld\Models\Countries::class);
}

并确保 ErnySans\Laraworld\Models\CountriesCountries 模型的正确完整 class 名称。

然后加载与相关实体和每个实体所在国家/地区的奖励建议:

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