在控制器中获取特定信息?



im 我的控制器和获取其中的数据有问题。我的问题是,当我让我的团队像这样时:

$team = Team::where('join_number', '=', $data['team'])->get();

我似乎不能通过去访问它

$team->join_request_needed = 'True'

例如?如果我只是死了转储

$team->join_request_needed

我得到一个"属性 [join_request_needed] 在此集合实例上不存在"。 如何访问这些数据?如果我死了转储$team我得到这个;所以它得到了数据?

IlluminateDatabaseEloquentCollection {#282 ▼
#items: array:1 [▼
0 => AppTeam {#291 ▼
#guarded: []
#connection: "sqlite"
#table: "teams"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:8 [▶]
#original: array:8 [▼
"id" => "1"
"user_id" => "1"
"name" => "team1"
"description" => null
"join_request_needed" => "0"
"join_number" => "1574832472024"
"created_at" => "2019-11-27 05:27:52"
"updated_at" => "2019-11-27 22:46:55"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
]
}

这是我的整个函数(到目前为止只是粗略的,因为我坚持获取数据(:

public function check() {
$data = request()->validate([
'email' => ['required', 'email'],
'team' => ['required', 'alpha_num', 'exists:teams,join_number']
]);
$team = Team::where('join_number', '=', $data['team'])->get();
if ($team->join_request_needed = 'True') {
if (Member::where('email', '=', $data['email'])->count() > 0) {
$member = Member::where('email', '=', $data['email']);
if ($member->approved = 'True') {
// member has been accepted
return view('portal.members.answer', [
'team' => $team
]); // questions
} else {
// member has not been accepted
dd('member has not been accepted');
return view(''); // waiting screen
}
} else {
// create a join request for the member
$team->member()->create([
'email' => $data['email']
]);
return view(''); // waiting screen
}
} else if(Member::where('email', '=', $data['email'])->count() > 0) {
// no join request needed, member already exists
return view('portal.members.answer', [
'team' => $team
]); // questions
} else {
// no join request needed, need to create member
dd($team);
$team->member()->create([
'email' => $data['email']
]);
return view('portal.members.answer', [
'team' => $team
]); // questions
}
}

团队在您的情况下是团队的集合,因此您可以使用查询构建器方法first()

$team = Team::where('join_number', '=', $data['team'])->first();

由于查询的工作方式,在查询生成器上调用 get 将始终返回集合。如果要first()单个对象,可以选择find()

由于您正在获取带有join_number = $data['team']Team集合 您需要先循环$team.但是,如果您只想拥有一个具有join_number = $data['team']的$team,则应first()而不是get()

相关内容

  • 没有找到相关文章

最新更新