WebForm.php 978 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace App\Models;
  3. /*
  4. * WebForm — модель веб-формы из конструктора форм.
  5. * Таблица: web_forms
  6. * Поле fields хранит JSON-массив объектов: [{label, name, type, width, required, options[]}, ...]
  7. * Связи: hasMany Lead
  8. */
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Database\Eloquent\Relations\HasMany;
  11. class WebForm extends Model
  12. {
  13. protected $fillable = ['title', 'slug', 'notify_email', 'fields', 'is_active'];
  14. protected $casts = [
  15. 'fields' => 'array',
  16. 'is_active' => 'boolean',
  17. ];
  18. // Заявки, поступившие через эту форму
  19. public function leads(): HasMany
  20. {
  21. return $this->hasMany(Lead::class, 'form_id');
  22. }
  23. // Количество непросмотренных заявок
  24. public function unreadLeadsCount(): int
  25. {
  26. return $this->leads()->whereNull('read_at')->count();
  27. }
  28. }