php - Laravel Eloquent ORM Conditional Relation -
i can't seem figure out.
i have 3 tables: user, experience, level (simplified)
user: {id, string:name} experience: {id, user_id, int:amount} level: {id, string:level, int:experience} //user class public function experiences(){ return $this->hasmany('experience', 'user_id'); }
there no database relations between experience , level.
how level, this:
public function level(){ return $this->experiences()->has('level.experience', '<', $this->experiences()->sum('amount'))->first(); }
i know how write join, i'd rather have eloquent me.
example data:
user: {1, 'userone'} experience: {1, 1, 10} level: {1, 'levelone', 5}
this user have level "levelone" - because sum of experience 10, , above level_id 1 minimum experience 5.
so have done, solve problem.
my user model:
public static function getlevel($experience){ $level = level::where('experience', '<=', $experience)->first(); return $level; }
a view:
{{auth::user()->getlevel(auth::user()->experiences->sum('amount'))->level}}
that best 'hack' come :)
Comments
Post a Comment