php - separating relationships and model functions in Laravel -
everytime i'm writing laravel model gives me feeling of messy code. have relationships , other model functions specially when using domain driven design. though separating relationships , functions.
example have user class extends eloqeunt:
class user extends eloquent{}
and inside class have register functions , password hashing functions etc. also, can declare relationships so:
class user extends eloquent{ function post(){ return $this->hasmany('post'); } }
for reason smells funky me. solution create entities folder , inside create user folder hold 2 files 1 userrelationship hold of the relationships class:
class userrelationship extends eloquent{ function post(){ return $this->hasmany('post'); } }
and second actual user class write of functions , class extend userrelationship class instead of eloquent:
class user extends userrelationship{ public static function register($email, $password, $activate_token) { $user = new static(compact('email', 'password', 'activate_token')); $user->raise(new userwasregistered($user)); return $user; } }
what guys think of approach relatively new don't know if bad practice or work little reward. guys recommend?
for user model, work. easiest way , still better approach define relationship in user model. if example post model have relationships post "user, comment, reply etc" can attempt splitting relationships
Comments
Post a Comment