Skip to content

Commit 6a615b6

Browse files
committed
Add firstOrFail method
1 parent 8e37aed commit 6a615b6

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/MessageQuery.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use DirectoryTree\ImapEngine\Support\ForwardsCalls;
1515
use DirectoryTree\ImapEngine\Support\Str;
1616
use Illuminate\Support\Collection;
17+
use Illuminate\Support\ItemNotFoundException;
1718
use Illuminate\Support\Traits\Conditionable;
1819

1920
/**
@@ -430,7 +431,19 @@ protected function populate(Collection $uids): MessageCollection
430431
*/
431432
public function first(): ?MessageInterface
432433
{
433-
return $this->limit(1)->get()->first();
434+
try {
435+
return $this->firstOrFail();
436+
} catch (ItemNotFoundException) {
437+
return null;
438+
}
439+
}
440+
441+
/**
442+
* Get the first message in the resulting collection or throw an exception.
443+
*/
444+
public function firstOrFail(): MessageInterface
445+
{
446+
return $this->limit(1)->get()->firstOrFail();
434447
}
435448

436449
/**

0 commit comments

Comments
 (0)