To avoid coupling the package with your application's business logic, keep models 'thin'. Models should primarily define structural properties like $table, $fillable, $hidden, $casts, $timestamps, and relationships.
Guidelines:
- Allowed: Simple static lookup methods (e.g.,
findByProvider()). - Avoid: Complex business logic or methods that perform side effects (e.g.,
linkUser()). - Where to put logic: Move complex operations to controllers, helpers, or dedicated service classes.
// Correct: Thin model focusing on structure
class SocialAccount extends Model
{
protected $fillable = [
'user_id', 'provider', 'provider_user_id', 'provider_email',
'provider_avatar', 'access_token', 'refresh_token', 'token_expires_at',
];
protected $hidden = [
'access_token', 'refresh_token',
];
protected $casts = [
'access_token' => EncryptedOrPlaintext::class,
'refresh_token' => EncryptedOrPlaintext::class,
'token_expires_at' => 'datetime',
];
public function user(): BelongsTo
{
return $this->belongsTo(config('tyro-login.user_model'));
}
}