patmarley
patmarley

Reputation: 45

Can't access desired attributes from Eloquent Collection

$result = User::where('email_add', '=', $username)->first();

Here's what shows up when I use var_dump on $result

object(User)#22 (30) { ["user_id"]=> int(0) ["fname"]=> string(0) "" ["lname"]=> string(0) "" ["mname"]=> string(0) "" ["bdate"]=> string(0) "" ["contact"]=> string(0) "" ["email_add"]=> string(0) "" ["fillable":protected]=> array(6) { [0]=> string(5) "fname" [1]=> string(5) "mname" [2]=> string(5) "lname" [3]=> string(5) "bdate" [4]=> string(7) "contact" [5]=> string(9) "email_add" } ["connection":protected]=> NULL ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(10) { ["user_id"]=> int(33) ["fname"]=> string(6) "Robert" ["lname"]=> string(6) "Marley" ["mname"]=> string(5) "Nesta" ["bdate"]=> string(10) "0000-00-00" ["contact"]=> string(11) "09123847599" ["email_add"]=> string(40) "7c9ac9ca8543dd84f71aa541d422e2a95d3e7450" ["photo"]=> NULL ["created_at"]=> string(19) "2017-01-25 01:03:06" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } ["original":protected]=> array(10) { ["user_id"]=> int(33) ["fname"]=> string(6) "Robert" ["lname"]=> string(6) "Marley" ["mname"]=> string(5) "Nesta" ["bdate"]=> string(10) "0000-00-00" ["contact"]=> string(11) "09123847599" ["email_add"]=> string(40) "7c9ac9ca8543dd84f71aa541d422e2a95d3e7450" ["photo"]=> NULL ["created_at"]=> string(19) "2017-01-25 01:03:06" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) }

Here's what happens when I use foreach to var_dump each $user on the $result

int(0) string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" bool(true) bool(true) bool(true) bool(false) 

I've been looking for a solution to this but I don't seem to find one, I was using get() at first but I read somewhere that if I'm expecting only 1 result, I should be using first() but it doesn't change anything. I can't access the Collection..

Upvotes: 1

Views: 533

Answers (1)

patmarley
patmarley

Reputation: 45

I've found a solution

This one helped me.

adding ->toArray on my ->first() was the magic, it only returns an array with the exact attributes without the unnecessary object properties.

Upvotes: 0

Related Questions