DashboardNote.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Str;
  6. class DashboardNote extends Model
  7. {
  8. use HasFactory;
  9. protected $fillable = [
  10. 'unique_id',
  11. 'text',
  12. 'completed',
  13. 'user_id'
  14. ];
  15. protected $casts = [
  16. 'completed' => 'boolean',
  17. 'created_at' => 'datetime',
  18. 'updated_at' => 'datetime'
  19. ];
  20. protected static function boot()
  21. {
  22. parent::boot();
  23. static::creating(function ($note) {
  24. if (empty($note->unique_id)) {
  25. $note->unique_id = Str::uuid();
  26. }
  27. });
  28. }
  29. public function user()
  30. {
  31. return $this->belongsTo(User::class);
  32. }
  33. public function scopeActive($query)
  34. {
  35. return $query->where('completed', false);
  36. }
  37. public function scopeForUser($query, $userId = null)
  38. {
  39. if ($userId) {
  40. return $query->where('user_id', $userId);
  41. }
  42. return $query->whereNull('user_id');
  43. }
  44. }