• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-chat / 22850766616

09 Mar 2026 11:13AM UTC coverage: 98.66% (-0.5%) from 99.187%
22850766616

push

github

web-flow
Merge pull request #57 from RonasIT/86c8kyana_Filter-chats-by-type

feat: Filter chats by type

3 of 5 new or added lines in 4 files covered. (60.0%)

368 of 373 relevant lines covered (98.66%)

25.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/src/Models/Conversation.php
1
<?php
2

3
namespace RonasIT\Chat\Models;
4

5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9
use Illuminate\Database\Eloquent\Relations\HasMany;
10
use Illuminate\Database\Eloquent\Relations\HasOne;
11
use RonasIT\Chat\Enums\Conversation\TypeEnum;
12
use RonasIT\Support\Traits\ModelTrait;
13

14
class Conversation extends Model
15
{
16
    use ModelTrait;
17

18
    protected $fillable = [
19
        'creator_id',
20
        'type',
21
        'title',
22
        'cover_id',
23
    ];
24

25
    protected $hidden = ['pivot'];
26

27
    protected $casts = [
28
        'type' => TypeEnum::class,
29
    ];
30

31
    public function last_message(): HasOne
32
    {
33
        return $this->hasOne(Message::class)->latest();
6✔
34
    }
35

36
    public function messages(): HasMany
37
    {
38
        return $this->hasMany(Message::class);
8✔
39
    }
40

41
    public function creator(): BelongsTo
42
    {
43
        return $this->belongsTo(config('chat.classes.user_model'));
6✔
44
    }
45

46
    public function members(): BelongsToMany
47
    {
48
        return $this->belongsToMany(
78✔
49
            related: config('chat.classes.user_model'),
78✔
50
            table: 'conversation_member',
78✔
51
            foreignPivotKey: 'conversation_id',
78✔
52
            relatedPivotKey: 'member_id',
78✔
53
        );
78✔
54
    }
55

56
    public function cover(): BelongsTo
57
    {
58
        return $this->belongsTo(config('chat.classes.media_model'), 'cover_id');
6✔
59
    }
60

61
    public function scopeWithUnreadMessagesCount(Builder $query, int $memberId): Builder
62
    {
63
        return $query->withCount([
2✔
64
            'messages as unread_messages_count' => fn ($query) => $query
2✔
65
                ->whereNot('sender_id', $memberId)
2✔
66
                ->whereDoesntHave('reads', fn ($query) => $query->where('read_messages.member_id', $memberId)),
2✔
67
        ]);
2✔
68
    }
69

70
    public function pinned_messages(): BelongsToMany
71
    {
72
        return $this
11✔
73
            ->belongsToMany(
11✔
74
                related: Message::class,
11✔
75
                table: 'pinned_messages',
11✔
76
                foreignPivotKey: 'conversation_id',
11✔
77
                relatedPivotKey: 'message_id',
11✔
78
            )
11✔
79
            ->orderByPivot('id', 'desc')
11✔
80
            ->withTimestamps();
11✔
81
    }
82

83
    public function hasMember(Model $member): bool
84
    {
85
        return $this->members()->where('member_id', $member->id)->exists();
34✔
86
    }
87

88
    public function isCreator(Model $member): bool
89
    {
90
        return $this->getAttribute('creator_id') === $member->id;
4✔
91
    }
92

93
    public function isGroup(): bool
94
    {
NEW
95
        return $this->getAttribute('type') === TypeEnum::Group;
×
96
    }
97

98
    public function isPrivate(): bool
99
    {
NEW
100
        return $this->getAttribute('type') === TypeEnum::Private;
×
101
    }
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc