- カテゴリ: collection
- 対応バージョン: Laravel 11・12/PHP 8.2(※Laravel 10には未収載)
- 名前空間 / FQCN / コマンド:
Illuminate\Support\Collection::after - 関連: before / search / first / last / get
- 変更履歴: Laravel 11.11 で
before/afterが追加。Laravel 10 には存在しない。(Laravel News, Laravel)
要点(TL;DR)
概要
after は、与えた値(または真偽判定クロージャ)に一致する最初の要素の直後の値を返します。見つからない、もしくは一致要素が末尾の場合は null を返します。before と対になる近接参照ユーティリティで、配列インデクス/連想キーに関わらず挿入順で判定します。(Laravel)
構文 / シグネチャ
mixed|null Collection::after(mixed|callable $needle, bool $strict = false)
- 引数
| 引数 | 型 | 必須 | 既定値 | 説明 |
|---|---|---|---|---|
$needle | mixed | callable | ✓ | — | 値を直接指定、または fn($item, $key): bool の真偽判定で一致位置を決める |
$strict | bool | false | 値一致時のみ有効。true で型まで一致(厳密比較)にする |
- 戻り値:
mixed|null— 直後の値。該当なし/末尾ならnull。(Laravel) - 例外/副作用:例外なし。コレクションは非破壊。
使用例
最小例
use Illuminate\Support\Collection;
$numbers = collect([1, 2, 3, 4, 5]);
$next = $numbers->after(3); // 4
$none = $numbers->after(5); // null(末尾のため)
$strict = collect([2,4,6,8])->after('4', strict: true); // null(型が違う)
// 既定はゆるい比較:collect([2,4,6,8])->after('4'); // 6 とならず 4 の直後 6 を返す動作に注意
(Laravel)
実務例(「現在のID」の次を取得して詳細画面へ遷移)
use Illuminate\Support\Collection;
use Illuminate\Http\RedirectResponse;
use App\Models\Post;
function nextPostRedirect(int $currentId): RedirectResponse {
// 公開順に並べてID配列を作成
/** @var Collection<int,int> $ids */
$ids = Post::query()->published()->orderByDesc('published_at')->pluck('id');
// 現在IDの「次」を取得(見つからなければ一覧へ)
$nextId = $ids->after($currentId);
return $nextId
? redirect()->route('posts.show', $nextId)
: redirect()->route('posts.index');
}
特性(コレクション)
- チェーン可否:不可(値を返すため)。
- 破壊的/非破壊:非破壊。
- キー保持:返すのは値(キーは返さない)。
- LazyCollection:対応(
LazyCollection::after()も利用可)。(Laravel News) - 計算量:O(n) — 先頭から線形探索。
入出力対応(例)
| 入力 | 呼び出し | 戻り値 |
|---|---|---|
[1,2,3,4,5] | after(3) | 4 |
[1,2,3,4,5] | after(5) | null |
[1,2,3,4,5,'name'=>'taylor','framework'=>'laravel'] | after('taylor') | 'laravel' |
| (Laravel News) |
よくある落とし穴・注意
- 比較の既定はゆるい比較。
'4'と4を区別したい場合はstrict: true。(Laravel) - 一致が複数あっても最初の一致の直後のみ返す。
- 末尾/未検出は
null。そのままメソッドチェーンせず、??などでハンドリング。 - バージョン差分:Laravel 11.11 以降。Laravel 10 にはない。(Laravel News, Laravel)
代替・関連APIとの比較
before:指定要素の直前を返す(対になるAPI)。隣接を両方向で扱いたい場合は併用。(Laravel)search+get:searchでキーを取り、次キーを自力計算してget…は連想キーやギャップで壊れやすい。afterの方が安全。(Laravel)sliding:隣り合う要素のウィンドウを列挙する用途に向く。単発で「次だけ欲しい」ならafterが簡潔。
テスト例(Pest)
use Illuminate\Support\Collection;
it('returns next item and null when not found or last', function () {
$c = collect([1,2,3]);
expect($c->after(2))->toBe(3);
expect($c->after(3))->toBeNull();
});
it('supports strict comparison and callback', function () {
expect(collect([2,4,6])->after('4', strict: true))->toBeNull();
$next = collect([2,4,6,8])->after(fn ($v) => $v > 5);
expect($next)->toBe(8);
});
トラブルシュート(エラー別)
| 症状/エラー | 原因 | 対処 |
|---|---|---|
Call to undefined method Illuminate\Support\Collection::after() | Laravel 10 以前 | Laravel 11.11 以降へアップグレード、または代替案(search+get 等)に置換。(Laravel, Laravel News) |
TypeError(null を渡した/期待) | 末尾で null が返っている | ?? / null 合体や条件分岐で未検出ケースを処理する |
想定外の一致('4' と 4 が同値扱い) | 既定がゆるい比較 | strict: true を指定する。(Laravel) |
参考リンク
- Laravel Docs — Collections: after / before(11.x / 12.x) (Laravel)
- Laravel News — Laravel 11.11 の新機能(
before/after追加) (Laravel News) - Laravel Versions — 11.11 の変更履歴(PR #51752 で
before/after追加) (Laravel Versions)
本記事は指定が無かったため、カテゴリ=collection/種類=メソッド/対応=Laravel 11・12/PHP 8.2 として記載しています。必要に応じて他バージョン追記も可能です。

