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

wimski / laravel-model-repositories / 29079599117

10 Jul 2026 08:22AM UTC coverage: 98.802% (+0.04%) from 98.765%
29079599117

push

github

wimski
Update Github action versions

165 of 167 relevant lines covered (98.8%)

12.8 hits per line

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

97.06
/src/Repositories/AbstractModelRepository.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Wimski\ModelRepositories\Repositories;
6

7
use Closure;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Database\Eloquent\Builder;
10
use Illuminate\Database\Eloquent\Collection;
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Database\Eloquent\ModelNotFoundException;
13
use Illuminate\Database\Query\Expression;
14
use Illuminate\Support\LazyCollection;
15
use Wimski\ModelRepositories\Contracts\Repositories\ModelRepositoryInterface;
16

17
/**
18
 * @template TModel of Model
19
 * @implements ModelRepositoryInterface<TModel>
20
 */
21
abstract readonly class AbstractModelRepository implements ModelRepositoryInterface
22
{
23
    /**
24
     * @param TModel $model
25
     */
26
    public function __construct(
27
        protected Model $model,
28
    ) {
29
    }
42✔
30

31
    public function builder(bool $withGlobalScopes = true): Builder
32
    {
33
        /** @var Builder<TModel> $builder */
34
        $builder = $this->model->newQueryWithoutScopes();
42✔
35

36
        if ($withGlobalScopes) {
42✔
37
            $this->model->registerGlobalScopes($builder);
41✔
38
        }
39

40
        return $builder;
42✔
41
    }
42

43
    public function find(int|string $key, string ...$column): ?Model
44
    {
45
        return $this->builder()->find($key, $this->parseColumns(...$column));
4✔
46
    }
47

48
    public function findOrFail(int|string $key, string ...$column): Model
49
    {
50
        return $this->builder()->findOrFail($key, $this->parseColumns(...$column));
4✔
51
    }
52

53
    public function findMany(Arrayable|array $keys, string ...$column): Collection
54
    {
55
        /** @var Collection<int, TModel> $result */
56
        $result = $this->builder()->findMany($keys, $this->parseColumns(...$column));
3✔
57

58
        return $result;
3✔
59
    }
60

61
    public function first(string ...$column): ?Model
62
    {
63
        return $this->builder()->first($this->parseColumns(...$column));
4✔
64
    }
65

66
    public function firstOrFail(string ...$column): Model
67
    {
68
        return $this->builder()->firstOrFail($this->parseColumns(...$column));
4✔
69
    }
70

71
    public function firstWhere(string|array|Closure|Expression $column, mixed $operator = null, mixed $value = null, string $boolean = 'and'): ?Model
72
    {
73
        return $this->builder()->firstWhere($column, $operator, $value, $boolean);
4✔
74
    }
75

76
    public function firstWhereOrFail(string|array|Closure|Expression$column, mixed $operator = null, mixed $value = null, string $boolean = 'and'): Model
77
    {
78
        $model = $this->firstWhere($column, $operator, $value, $boolean);
2✔
79

80
        if ($model === null) {
2✔
81
            throw $this->makeModelNotFoundException();
1✔
82
        }
83

84
        return $model;
1✔
85
    }
86

87
    public function where(string|array|Closure|Expression$column, mixed $operator = null, mixed $value = null, string $boolean = 'and'): Collection
88
    {
89
        /** @var Collection<int, TModel> $result */
90
        $result = $this->builder()->where($column, $operator, $value, $boolean)->get();
1✔
91

92
        return $result;
1✔
93
    }
94

95
    public function whereIn(string $column, array $values): Collection
96
    {
97
        /** @var Collection<int, TModel> $result */
98
        $result = $this->builder()->whereIn($column, $values)->get();
1✔
99

100
        return $result;
1✔
101
    }
102

103
    public function whereNotIn(string $column, array $values): Collection
104
    {
105
        /** @var Collection<int, TModel> $result */
106
        $result = $this->builder()->whereNotIn($column, $values)->get();
1✔
107

108
        return $result;
1✔
109
    }
110

111
    public function cursor(): LazyCollection
112
    {
113
        return $this->builder()->cursor();
1✔
114
    }
115

116
    public function all(string ...$column): Collection
117
    {
118
        /** @var Collection<int, TModel> $result */
119
        $result = $this->builder()->get($this->parseColumns(...$column));
3✔
120

121
        return $result;
3✔
122
    }
123

124
    public function make(array $attributes): Model
125
    {
126
        return $this->builder()->make($attributes);
1✔
127
    }
128

129
    public function findOrMake(int|string $key, string ...$column): Model
130
    {
131
        return $this->builder()->findOrNew($key, $this->parseColumns(...$column));
4✔
132
    }
133

134
    public function firstWhereOrMake(array $attributes, array $values = []): Model
135
    {
136
        return $this->builder()->firstOrNew($attributes, $values);
2✔
137
    }
138

139
    public function create(array $attributes): Model
140
    {
141
        return $this->builder()->create($attributes);
1✔
142
    }
143

144
    public function firstWhereOrCreate(array $attributes, array $values = []): Model
145
    {
146
        return $this->builder()->firstOrCreate($attributes, $values);
2✔
147
    }
148

149
    /**
150
     * @return array<int, string>
151
     */
152
    protected function parseColumns(string ...$column): array
153
    {
154
        return empty($column) ? ['*'] : array_values($column);
26✔
155
    }
156

157
    /**
158
     * @throws ModelNotFoundException
159
     */
160
    protected function throwModelNotFoundException(int|string ...$key): void
161
    {
162
        throw $this->makeModelNotFoundException(...$key);
×
163
    }
164

165
    /**
166
     * @return ModelNotFoundException<TModel>
167
     */
168
    protected function makeModelNotFoundException(int|string ...$key): ModelNotFoundException
169
    {
170
        /** @var ModelNotFoundException<TModel> $exception */
171
        $exception = (new ModelNotFoundException())->setModel(get_class($this->model), array_values($key));
1✔
172

173
        return $exception;
1✔
174
    }
175
}
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