Reputation: 99
im having issues with my controller and getting data within it. My issue is that when i get my team like so:
$team = Team::where('join_number', '=', $data['team'])->get();
i cant seem too access it by going
$team->join_request_needed = 'True'
for example? If i just die dump
$team->join_request_needed
i get a 'Property [join_request_needed] does not exist on this collection instance'. How can i access this data? If i die dump $team i get this; so it gets the data?
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: []
}
]
}
Here is my entire function (only rough so far since im stuck on getting the data):
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
}
}
Upvotes: 0
Views: 41
Reputation: 29
Since you are getting the collection of Team
with join_number = $data['team']
You need to loop first for the $team
. But if you only want to have one $team that has a join_number = $data['team']
you should do first()
instead of get()
Upvotes: 0
Reputation: 18976
Team is in your case a collection of Teams, therefor you can utilise the querybuilder method first()
$team = Team::where('join_number', '=', $data['team'])->first();
Since the way queries work, calling get on the Query Builder will always return a collection. If you want single objects first()
or find()
is the options.
Upvotes: 1