PHP標準関数とLaravelのヘルパ/メソッドを「同じ目的ごと」に並べた対応表(チートシート)です。
すべてのサンプルは php artisan tinker にそのままコピペで動きます。
環境:PHP 8.5 / Laravel 12 / Carbon 3系 / Tinker 起動 →
Laravel系のクラスは基本
php artisan tinkerLaravel系のクラスは基本
use 無しでTinker上から呼べます(Str::upper('a') などそのまま)。本記事では明示的に use を書いていますが、Tinkerでは省略してもOKです。
1. 文字列操作
1-1. 部分一致(含む)
PHP
// str_contains は PHP 8.0+ の標準関数
str_contains('Laravel12 is cool', 'Laravel');
// => true
Laravel
use Illuminate\Support\Str;
Str::contains('Laravel12 is cool', 'Laravel');
// => true
// 複数条件(どれか含めばtrue)もいける
Str::contains('Laravel12 is cool', ['PHP', 'Laravel']);
// => true
1-2. 前方/後方一致
PHP
str_starts_with('https://example.com', 'https://'); // true
str_ends_with('report.pdf', '.pdf'); // true
Laravel
use Illuminate\Support\Str;
Str::startsWith('https://example.com', 'https://'); // true
Str::endsWith('report.pdf', ['.pdf', '.docx']); // true(配列OK)
1-3. 置換
PHP
str_replace('world', 'Laravel', 'hello world');
// => "hello Laravel"
Laravel
use Illuminate\Support\Str;
Str::replace('world', 'Laravel', 'hello world');
// => "hello Laravel"
// 最初/最後の1回だけ置換できるのが便利
Str::replaceFirst('a', 'X', 'banana'); // "bXnana"
Str::replaceLast('a', 'X', 'banana'); // "bananX"
1-4. 大文字/小文字/スネーク/キャメル
PHP
strtoupper('laravel'); // "LARAVEL"
strtolower('LARAVEL'); // "laravel"
ucfirst('laravel'); // "Laravel"
ucwords('hello world'); // "Hello World"
Laravel
use Illuminate\Support\Str;
Str::upper('laravel'); // "LARAVEL"
Str::lower('LARAVEL'); // "laravel"
Str::ucfirst('laravel'); // "Laravel"
Str::title('hello world'); // "Hello World"
// PHPにはないケース変換
Str::snake('helloWorld'); // "hello_world"
Str::camel('hello_world'); // "helloWorld"
Str::kebab('helloWorld'); // "hello-world"
Str::studly('hello_world'); // "HelloWorld"
1-5. トリム/文字数/切り詰め
PHP
trim(' hello '); // "hello"
mb_strlen('あいうえお'); // 5 (マルチバイト対応)
mb_substr('あいうえお', 0, 3);// "あいう"
Laravel
use Illuminate\Support\Str;
Str::length('あいうえお'); // 5
Str::substr('あいうえお', 0, 3); // "あいう"
// 末尾に「...」を付けて切り詰め、PHPに同等関数はない
Str::limit('長い本文テキストです', 5); // "長い本文テ..."
1-6. ランダム文字列/UUID/ULID
PHP
bin2hex(random_bytes(8)); // ランダム16文字hex
// UUIDはPHPだと手書きか拡張が必要
Laravel
use Illuminate\Support\Str;
Str::random(16); // 英数字16文字
(string) Str::uuid(); // UUIDv4
(string) Str::ulid(); // ULID(時系列ソート可)
1-7. PHP 8.5 のパイプ演算子 |>
PHP 8.5 から使える新構文。関数を左→右に繋げられるので、ネストが深い処理が読みやすくなる。
// 従来:ネストして読みにくい
$slug = strtolower(str_replace(' ', '-', trim(' Hello World ')));
// => "hello-world"
// PHP 8.5 パイプ演算子(1行で書けばTinkerでもそのまま動く)
// ※アロー関数は必ず () で囲む(囲まないと構文エラーになる)
$slug = ' Hello World ' |> trim(...) |> (fn($s) => str_replace(' ', '-', $s)) |> strtolower(...);
// => "hello-world"
2. 配列操作
2-1. 最初/最後の値を取る
PHP 8.5 で
array_first() / array_last() が標準関数に追加されました。
PHP 8.5
$a = ['a', 'b', 'c'];
array_first($a); // "a"
array_last($a); // "c"
// 従来のやり方(8.4以前)
reset($a); // "a"
end($a); // "c"
Laravel
collect(['a', 'b', 'c'])->first(); // "a"
collect(['a', 'b', 'c'])->last(); // "c"
// 条件付きもワンライナー
collect([1, 2, 3, 4])->first(fn($v) => $v > 2); // 3
2-2. map / filter / reduce
PHP
$nums = [1, 2, 3, 4, 5];
array_map(fn($n) => $n * 2, $nums); // [2,4,6,8,10]
array_filter($nums, fn($n) => $n % 2 === 0); // [1=>2, 3=>4]
array_reduce($nums, fn($c, $n) => $c + $n, 0); // 15
Laravel
// メソッドチェーンは1行で(Tinker対策)
collect([1, 2, 3, 4, 5])->map(fn($n) => $n * 2)->filter(fn($n) => $n > 4)->values()->all();
// => [6, 8, 10]
collect([1, 2, 3, 4, 5])->sum(); // 15
collect([1, 2, 3, 4, 5])->avg(); // 3
2-3. 連想配列:キー取得・値取得
PHP
$user = ['name' => 'Taro', 'age' => 30];
array_keys($user); // ["name", "age"]
array_values($user); // ["Taro", 30]
array_key_exists('name', $user); // true
Laravel
use Illuminate\Support\Arr;
$user = ['name' => 'Taro', 'age' => 30];
Arr::has($user, 'name'); // true
Arr::get($user, 'name'); // "Taro"
Arr::get($user, 'email', 'なし'); // デフォルト値指定できる
2-4. ネストされた配列にドット記法でアクセス
ここはLaravelが圧倒的に楽。設定ファイルやAPIレスポンスの扱いで多用する。
PHP
$data = ['user' => ['profile' => ['name' => 'Taro']]];
$data['user']['profile']['name'] ?? 'default'; // "Taro"
Laravel
use Illuminate\Support\Arr;
$data = ['user' => ['profile' => ['name' => 'Taro']]];
Arr::get($data, 'user.profile.name'); // "Taro"
Arr::get($data, 'user.profile.email', 'default'); // "default"
data_get($data, 'user.profile.name'); // 同上(関数版)
2-5. 指定キーだけ/除外して抜き出す
PHP
$user = ['name' => 'Taro', 'email' => 'a@b.c', 'password' => 'x'];
array_intersect_key($user, array_flip(['name', 'email']));
// => ["name" => "Taro", "email" => "a@b.c"]
array_diff_key($user, array_flip(['password']));
// => ["name" => "Taro", "email" => "a@b.c"]
Laravel
use Illuminate\Support\Arr;
$user = ['name' => 'Taro', 'email' => 'a@b.c', 'password' => 'x'];
Arr::only($user, ['name', 'email']); // name, emailのみ
Arr::except($user, ['password']); // passwordを除く
2-6. 特定カラムだけ抜き出す(pluck)
PHP
$users = [['id' => 1, 'name' => 'Taro'], ['id' => 2, 'name' => 'Hana']];
array_column($users, 'name'); // ["Taro", "Hana"]
array_column($users, 'name', 'id'); // [1 => "Taro", 2 => "Hana"]
Laravel
$users = [['id' => 1, 'name' => 'Taro'], ['id' => 2, 'name' => 'Hana']];
collect($users)->pluck('name')->all(); // ["Taro", "Hana"]
collect($users)->pluck('name', 'id')->all(); // [1=>"Taro", 2=>"Hana"]
2-7. ソート
PHP
$a = [3, 1, 2];
sort($a); // 破壊的:[1,2,3]
rsort($a); // 降順:[3,2,1]
$users = [['age' => 30], ['age' => 20]];
usort($users, fn($a, $b) => $a['age'] <=> $b['age']);
Laravel
collect([3, 1, 2])->sort()->values()->all(); // [1,2,3]
collect([3, 1, 2])->sortDesc()->values()->all(); // [3,2,1]
$users = [['age' => 30], ['age' => 20]];
collect($users)->sortBy('age')->values()->all(); // キー指定でソート、非破壊
2-8. グループ化
PHP
// PHP標準には groupBy が無いので自作
$users = [['role' => 'admin', 'name' => 'A'], ['role' => 'user', 'name' => 'B'], ['role' => 'admin', 'name' => 'C']];
$grouped = [];
foreach ($users as $u) { $grouped[$u['role']][] = $u; }
$grouped;
// [admin => [...], user => [...]]
Laravel
$users = [['role' => 'admin', 'name' => 'A'], ['role' => 'user', 'name' => 'B'], ['role' => 'admin', 'name' => 'C']];
collect($users)->groupBy('role')->all(); // 一行で終わる
3. 数値・フォーマット
PHP
number_format(1234567); // "1,234,567"
number_format(1234.5, 2); // "1,234.50"
round(1.567, 2); // 1.57
max(1, 2, 3); // 3
min([1, 2, 3]); // 1
Laravel
use Illuminate\Support\Number;
Number::format(1234567); // "1,234,567"
Number::format(1234.5, precision: 2); // "1,234.50"
Number::currency(1234.56, 'JPY'); // "¥1,235"
Number::fileSize(1024 * 1024); // "1 MB"
Number::forHumans(1230000, precision: 2); // "1.23 million"
Number::ordinal(3); // "3rd"
Number::abbreviate(12345); // "12K"
4. 日付操作
PHP
date('Y-m-d H:i:s'); // 現在時刻
date('Y-m-d', strtotime('+3 days')); // 3日後
$d = new DateTime('2026-04-20');
$d->modify('+1 month');
$d->format('Y-m-d'); // "2026-05-20"
$diff = (new DateTime('2026-01-01'))->diff(new DateTime('2026-04-20'));
$diff->days; // 109
Laravel (Carbon)
use Illuminate\Support\Carbon;
Carbon::now()->format('Y-m-d H:i:s');
Carbon::now()->addDays(3)->toDateString(); // 3日後
Carbon::now()->subMonth()->toDateString(); // 1ヶ月前
// ※Carbon 3(Laravel 12標準)では diffIn系はfloatを返す
Carbon::parse('2026-04-20')->diffInDays(Carbon::parse('2026-01-01')); // -109.0(符号に注意)
// 絶対値で欲しい時
Carbon::parse('2026-04-20')->diffInDays(Carbon::parse('2026-01-01'), absolute: true); // 109.0
// 日本語も簡単
Carbon::now()->locale('ja')->isoFormat('LLLL'); // "2026年4月20日月曜日 午後5時30分" のように
Carbon::now()->diffForHumans(); // "5 minutes ago" / "5 分前"
5. 値チェック(空かどうか)
empty() と blank() の違いに注意。'0' を空と判定するかしないかが違う。
PHP
empty(''); // true
empty('0'); // true ← ここが罠
empty(null); // true
empty([]); // true
$x = 1;
isset($x); // true (変数が存在するか)
Laravel
blank(''); // true
blank('0'); // false ← '0'は値と見なす
blank(null); // true
blank([]); // true
blank(' '); // true ← 空白のみもtrue
filled('0'); // true
filled(''); // false
6. JSON / ダンプ
PHP
$data = ['name' => '太郎', 'age' => 30];
json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
json_decode('{"name":"太郎"}', true); // 連想配列で受け取る
var_dump($data);
print_r($data);
Laravel
$data = ['name' => '太郎', 'age' => 30];
collect($data)->toJson(JSON_UNESCAPED_UNICODE);
dump($data); // 整形されて表示、処理は続く
// dd($data); // 表示して処理停止(die and dump)※Tinkerでは使わない方が無難
7. ファイル操作
PHP
file_put_contents('/tmp/a.txt', 'hello');
file_get_contents('/tmp/a.txt'); // "hello"
file_exists('/tmp/a.txt'); // true
unlink('/tmp/a.txt'); // 削除
Laravel
use Illuminate\Support\Facades\Storage;
// デフォルトディスク(config/filesystems.php で設定)配下で扱う
Storage::put('a.txt', 'hello');
Storage::get('a.txt');
Storage::exists('a.txt');
Storage::delete('a.txt');
// ディスク指定もできる(S3とかpublicとか)
Storage::disk('public')->url('a.txt');
8. HTTP(外部API呼び出し)
PHP
// GET
$res = file_get_contents('https://httpbin.org/get');
// cURL版(POST)
$ch = curl_init('https://httpbin.org/post');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(['x' => 1]),
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
]);
$res = curl_exec($ch);
curl_close($ch);
$res;
Laravel
use Illuminate\Support\Facades\Http;
// GET(変数で受ければチェーンしなくてOK)
$res = Http::get('https://httpbin.org/get');
$res->json(); // 連想配列で取得
$res->status(); // 200
$res->ok(); // true
// POST + JSON は1行で
$res = Http::acceptJson()->withToken('xxx')->post('https://httpbin.org/post', ['x' => 1]);
9. バリデーション
PHP
filter_var('a@b.com', FILTER_VALIDATE_EMAIL); // "a@b.com"
filter_var('abc', FILTER_VALIDATE_EMAIL); // false
filter_var('https://a.com', FILTER_VALIDATE_URL);
filter_var('123', FILTER_VALIDATE_INT); // 123
Laravel
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
$v = Validator::make(['email' => 'a@b.com', 'age' => 20], ['email' => 'required|email', 'age' => 'required|integer|min:18']);
$v->fails(); // false
$v->errors(); // エラーメッセージ
// 単発の形式チェックなら
Str::isUuid('xxx'); // false
Str::isUrl('https://a.com'); // true
Str::isJson('{"a":1}'); // true
10. ハッシュ化・暗号化
PHP
$h = password_hash('secret', PASSWORD_BCRYPT);
password_verify('secret', $h); // true
md5('abc'); // "900150983cd24fb0..."
hash('sha256', 'abc');
Laravel
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Crypt;
$h = Hash::make('secret');
Hash::check('secret', $h); // true
// 可逆暗号(APP_KEY使用)
$enc = Crypt::encryptString('hello');
Crypt::decryptString($enc); // "hello"
11. Tinkerで動かす時のコツ
$ php artisan tinker
Psy Shell v0.x.x (PHP 8.5.x — cli) by Justin Hileman
> Str::slug('Hello World 2026')
= "hello-world-2026"
> collect([3,1,2])->sort()->values()->all()
= [1, 2, 3]
> exit
まとめ
使い分けの指針
・PHP標準で済むものはPHPで書く(軽くて速い)
・チェーンしたい/ネスト配列はLaravel(
・日付・通貨・ファイルサイズのフォーマットもLaravel(
・PHP 8.5のパイプ演算子とarray_first/lastは覚えると日常のコードがすっきりします
・PHP標準で済むものはPHPで書く(軽くて速い)
・チェーンしたい/ネスト配列はLaravel(
collect(), Arr::get())が圧倒的に楽・日付・通貨・ファイルサイズのフォーマットもLaravel(
Carbon, Number)が強い・PHP 8.5のパイプ演算子とarray_first/lastは覚えると日常のコードがすっきりします
迷ったら tinker でパッと試してみるのが一番早いです。
