我的控制器有问题并在其中获取数据。我的问题是,当我像这样让我的团队时:
$team = Team::where('join_number', '=', $data['team'])->get();
我似乎无法通过去访问它
$team->join_request_needed = 'True'
例如?如果我只是死转储
$team->join_request_needed
我收到“此集合实例上不存在属性 [join_request_needed]”。我如何访问这些数据?如果我死了转储$team,我会得到这个;所以它得到数据?
Illuminate\Database\Eloquent\Collection {#282 ▼
#items: array:1 [▼
0 => App\Team {#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
}
}