php - In Laravel Eloquent, why is this not selecting properly (no where condition)? -


using this:

profile = user::find($user)->with('profile')->first();

will return query of select * profiles, instead of selecting profile belonging id of found user.

my user model follows:

public function profile() {     return $this->hasone('app\models\userprofile', 'user_id'); } 

why not adding where condition?

what find($id) expand query, this:

// user::find($user) // user::where('id', $user)->take(1)->get('*')->first() 

in other words: creates query, limits first row, gets row - returns collection - , returns first item in collection.

that means with('profile') gets attached object, not query builder, , tack first() call that.

what should instead is

user::with('profile')->find($user); 

... assume works haven't tested it. :)


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -