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
Post a Comment