我遇到了我认为很简单的事情,我只是想多了。我在 Tinker 会话中运行以下命令,它按预期工作:
$game = Games::find(1);
[!] Aliasing 'Games' to 'App\Models\Games' for this Tinker session.
=> App\Models\Games {#4386
id: 1,
user_id: 1,
title: "Test Game",
description: "This is a test of the game function",
max_players: 8,
deck: "default",
type: "Golf",
privacy: "Public",
current_player: null,
status: "pending",
deleted_at: null,
created_at: "2020-12-18 22:02:17",
updated_at: "2020-12-18 22:02:17",
}
>>> $game->players()->get();
=> Illuminate\Database\Eloquent\Collection {#4322
all: [
App\Models\User {#4384
id: 1,
name: "mark",
email: "test@test.com",
username: "user",
role: null,
email_verified_at: null,
created_at: "2020-12-18 22:02:08",
updated_at: "2020-12-18 22:02:08",
pivot: Illuminate\Database\Eloquent\Relations\Pivot {#4168
games_id: 1,
user_id: 1,
},
},
],
}
我基本上已经在我的控制器中放置了完全相同的代码来拉取游戏中的玩家列表:
$game = Games::find($game);
$players = $game->players()->get();
当我到达路线时,我得到了这个:
Method Illuminate\Database\Eloquent\Collection::players does not exist.
我很困惑,如果它在 Tinker 中工作得很好,为什么它不能在控制器中工作。
谢谢您的帮助!