Skip to content

Commit 5ba19a2

Browse files
[9.x] Let Multiple* exceptions hold the number of records and items found (#41164)
* Let Multiple* exceptions hold the number of records and items found. * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent 7e0d9a6 commit 5ba19a2

File tree

8 files changed

+81
-14
lines changed

8 files changed

+81
-14
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,12 +1208,14 @@ public function sole($key = null, $operator = null, $value = null)
12081208

12091209
$items = $this->unless($filter == null)->filter($filter);
12101210

1211-
if ($items->isEmpty()) {
1211+
$count = $items->count();
1212+
1213+
if ($count === 0) {
12121214
throw new ItemNotFoundException;
12131215
}
12141216

1215-
if ($items->count() > 1) {
1216-
throw new MultipleItemsFoundException;
1217+
if ($count > 1) {
1218+
throw new MultipleItemsFoundException($count);
12171219
}
12181220

12191221
return $items->first();

src/Illuminate/Collections/MultipleItemsFoundException.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,35 @@
66

77
class MultipleItemsFoundException extends RuntimeException
88
{
9+
/**
10+
* The number of items found.
11+
*
12+
* @var int
13+
*/
14+
public $count;
15+
16+
/**
17+
* Create a new exception instance.
18+
*
19+
* @param int $count
20+
* @param int $code
21+
* @param \Throwable|null $previous
22+
* @return void
23+
*/
24+
public function __construct($count, $code = 0, $previous = null)
25+
{
26+
$this->count = $count;
27+
28+
parent::__construct("$count items were found.", $code, $previous);
29+
}
30+
31+
/**
32+
* Get the number of items found.
33+
*
34+
* @return int
35+
*/
36+
public function getCount()
37+
{
38+
return $this->count;
39+
}
940
}

src/Illuminate/Database/Concerns/BuildsQueries.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,14 @@ public function sole($columns = ['*'])
309309
{
310310
$result = $this->take(2)->get($columns);
311311

312-
if ($result->isEmpty()) {
312+
$count = $result->count();
313+
314+
if ($count === 0) {
313315
throw new RecordsNotFoundException;
314316
}
315317

316-
if ($result->count() > 1) {
317-
throw new MultipleRecordsFoundException;
318+
if ($count > 1) {
319+
throw new MultipleRecordsFoundException($count);
318320
}
319321

320322
return $result->first();

src/Illuminate/Database/Eloquent/Relations/Relation.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,14 @@ public function sole($columns = ['*'])
171171
{
172172
$result = $this->take(2)->get($columns);
173173

174-
if ($result->isEmpty()) {
174+
$count = $result->count();
175+
176+
if ($count === 0) {
175177
throw (new ModelNotFoundException)->setModel(get_class($this->related));
176178
}
177179

178-
if ($result->count() > 1) {
179-
throw new MultipleRecordsFoundException;
180+
if ($count > 1) {
181+
throw new MultipleRecordsFoundException($count);
180182
}
181183

182184
return $result->first();

src/Illuminate/Database/MultipleRecordsFoundException.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,35 @@
66

77
class MultipleRecordsFoundException extends RuntimeException
88
{
9-
//
9+
/**
10+
* The number of records found.
11+
*
12+
* @var int
13+
*/
14+
public $count;
15+
16+
/**
17+
* Create a new exception instance.
18+
*
19+
* @param int $count
20+
* @param int $code
21+
* @param \Throwable|null $previous
22+
* @return void
23+
*/
24+
public function __construct($count, $code = 0, $previous = null)
25+
{
26+
$this->count = $count;
27+
28+
parent::__construct("$count records were found.", $code, $previous);
29+
}
30+
31+
/**
32+
* Get the number of records found.
33+
*
34+
* @return int
35+
*/
36+
public function getCount()
37+
{
38+
return $this->count;
39+
}
1040
}

tests/Integration/Database/EloquentWhereTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function testSoleFailsForMultipleRecords()
115115
'address' => 'other-address',
116116
]);
117117

118-
$this->expectException(MultipleRecordsFoundException::class);
118+
$this->expectExceptionObject(new MultipleRecordsFoundException(2));
119119

120120
UserWhereTest::where('name', 'test-name')->sole();
121121
}

tests/Integration/Database/QueryBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testSoleFailsForMultipleRecords()
4040
['title' => 'Foo Post', 'content' => 'Lorem Ipsum.', 'created_at' => new Carbon('2017-11-12 13:14:15')],
4141
]);
4242

43-
$this->expectException(MultipleRecordsFoundException::class);
43+
$this->expectExceptionObject(new MultipleRecordsFoundException(2));
4444

4545
DB::table('posts')->where('title', 'Foo Post')->sole();
4646
}

tests/Support/SupportCollectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function testSoleThrowsExceptionIfNoItemsExist($collection)
104104
*/
105105
public function testSoleThrowsExceptionIfMoreThanOneItemExists($collection)
106106
{
107-
$this->expectException(MultipleItemsFoundException::class);
107+
$this->expectExceptionObject(new MultipleItemsFoundException(2));
108108

109109
$collection = new $collection([
110110
['name' => 'foo'],
@@ -146,7 +146,7 @@ public function testSoleThrowsExceptionIfNoItemsExistWithCallback($collection)
146146
*/
147147
public function testSoleThrowsExceptionIfMoreThanOneItemExistsWithCallback($collection)
148148
{
149-
$this->expectException(MultipleItemsFoundException::class);
149+
$this->expectExceptionObject(new MultipleItemsFoundException(2));
150150

151151
$data = new $collection(['foo', 'bar', 'bar']);
152152

0 commit comments

Comments
 (0)