Bad Practice Example
class User extends Model { public function attempts() { return $this->hasMany(UserAttempt::class); } public function record_attempts(String $attempt) { (new UserAttempt([ 'attempt' => $attempt, 'user_id' => $this->id, 'ip' => request()->ip() ]))->save(); } }
What To Do Instead
namespace App\Services; class UserService { public function record_attempts(String $attempt) { (new UserAttempt([ 'attempt' => $attempt, 'user_id' => $this->id, 'ip' => request()->ip() ]))->save(); } } class User extends Model { public function attempts() { return $this->hasMany(UserAttempt::class); } }
الـ Model يفترض أنه يحتوي فقط على الدوال التي لها علاقة بقاعدة البيانات fillable, hidden, casts, relationships…، وليس من المفضل أن يتم وضع أي logic به.
لكن الـ logic يفضل ان يتم وضعه في Service Class.