📚
Tinkerについて
開発環境
- macOS
- VSCode
- PHP 8.0.30
- Laravel 9.52.16
- Tinker 0.12.0
行いたいこと
- Laravelでの実装過程で
Tinker(ティンカー)
について学習したためまとめていく。
Tinkerとは?
・Tinker
とはphpのフレームワークLaravel
に標準搭載されている対話型シェル
です。
・Tinker GitHub
対話型シェル(REPL)
対話型シェル
とはREPL(Read-Eval-Print Loop)
のことで、入力したコマンドやコードを逐次的に読み取り(Read)
、それを評価・実行し(Eval)
、その結果を表示(Print)
して、次の入力を待つ動作を繰り返す(Loop)
仕組みの対話型実行環境
です。
Tinkerで出来ること
・コントローラを試しに動かす。
・データベースからレコードを取得する。
・新しいモデルインスタンス作成する。
・バリデーションのテストをする。
など、Tinkerは開発時のデバッグや動作確認に便利なツールで、本番環境に影響を与えることなく様々な機能をテストできます。
Tinkerの使い方
・Tinkerを起動する。
ターミナル
$ php artisan tinker
・Tinkerを終了する。
> exit or control + c
それぞれのバージョンを確認する。
php artisan tinker
で起動してから
・Tinker
> Psy\Shell::VERSION
= "v0.12.0"
・PHP
> echo PHP_VERSION;
8.0.30⏎
・Laravel
> app()->version();
= "9.52.16"
・使用可能なコマンド一覧を表示する。
> help
実行コードと役割解説
・help
実行コード
macbook@MacBook-Air laravel % docker compose exec app php artisan tinker
Psy Shell v0.12.0 (PHP 8.0.30 — cli) by Justin Hileman
> help
help Show a list of commands. Type `help [foo]` for information about [foo]. Aliases: ?
ls List local, instance or class variables, methods and constants. Aliases: dir
dump Dump an object or primitive.
doc Read the documentation for an object, class, constant, method or property. Aliases: rtfm, man
show Show the code for an object, class, constant, method or property.
wtf Show the backtrace of the most recent exception. Aliases: last-exception, wtf?
whereami Show where you are in the code.
throw-up Throw an exception or error out of the Psy Shell.
timeit Profiles with a timer.
trace Show the current call stack.
buffer Show (or clear) the contents of the code input buffer. Aliases: buf
clear Clear the Psy Shell screen.
edit Open an external editor. Afterwards, get produced code in input buffer.
sudo Evaluate PHP code, bypassing visibility restrictions.
history Show the Psy Shell history. Aliases: hist
exit End the current session and return to caller. Aliases: quit, q
migrate Run the database migrations
migrate:install Create the migration repository
inspire Display an inspiring quote
clear-compiled Remove the compiled class file
down Put the application into maintenance / demo mode
env Display the current framework environment
optimize Cache the framework bootstrap files
up Bring the application out of maintenance mode
>
・それぞれの役割(help コマンド名
で情報表示)
help コマンド一覧を表示。
以下`help コマンド名`と入力すると'コマンド名'についての情報を表示。
<例> `help ls`
ls ローカル変数、インスタンス変数、クラス変数、メソッド、定数を一覧表示する。
dump オブジェクトまたはプリミティブ型をダンプ表示する。
doc オブジェクト、クラス、定数、メソッド、プロパティのドキュメント表示する。
show オブジェクト、クラス、定数、メソッド、プロパティのコードを表示する。
wtf 最新の例外のバックトレースを表示する。
whereami 現在のコード上の位置を表示する。
throw-up 現在のPsy Shellセッション内で例外やエラーをスロー(再送出)する。
timeit 処理時間を計測してパフォーマンスをプロファイリングする。
trace 現在の呼び出しスタック(関数やメソッドの呼び出し履歴)を表示する。
buffer コード入力バッファの内容を表示またはクリアする。
clear Psy Shellの画面をクリアする。
edit 外部エディタでコードを開き、編集する。編集結果は入力バッファに取り込まれる。
sudo 可視性制限を迂回してPHPコードを評価する。
history Psy Shell内で実行されたコマンドの履歴を表示する。
exit 現在のTinkerセッションを終了してシェルを抜ける。
migrate データベースのマイグレーションを実行する。
migrate:install マイグレーションリポジトリを作成する。
inspire ランダムな心に響く言葉を表示する。
clear-compiled コンパイル済みクラスファイルを削除する。
down アプリケーションをメンテナンスモードにする。
env 現在のフレームワーク環境を表示する。
optimize フレームワークのブートストラップファイルをキャッシュしてパフォーマンスを向上させる。
up アプリケーションをメンテナンスモードから復帰させる。
コマンドの使用例
・コマンドの履歴を確認する
> history
0: ::all();
1: echo PHP_VERSION;
2: echo LAEAVER_VERSION;
3: app()->version();
4: Board::all();
5: help
6: phpversion();
7: help trace
8: dump
9: help dump
10: Psy\Shell::VERSION
・1つのカラムに新しいデータを追加する
$user = new App\Models\User;
$user->name = 'Test User';
$user->save();
複数のカラムにデータを追加する
$user = new App\Models\User;
$user->name = 'Test User';
$user->email = 'test@example.com';
$user->password = bcrypt('password');
$user->save();
・特定のIDを指定してデータを検索する
・例としてusersテーブルから特定のIDを検索
$user = App\Models\User::find(1);
出力例
> $user = App\Models\User::find(1);
= App\Models\User {#6003
id: 1,
name: "Test User",
email: "test@example.com",
email_verified_at: null,
#password: "$2y$10$WlB3H9vpUICybnLcZAOSVuyFoZtI2iL8qbVVYYokkJ4Oh.UTwnj4",
#remember_token: "TNgIe4ycIozB3ansEVEbpYVWGgn7N5nMi4kQLMFDxSXJybSA81rIc5YLw3I",
created_at: "2024-11-02 14:04:15",
updated_at: "2024-11-02 14:04:15",
}
>
pluck()
メソッド)
・特定のカラムのみを抽出する(・例として全てのユーザーのnameのみを取得する
$emails = App\Models\User::pluck('name');
出力例
> $emails = App\Models\User::pluck('name');
= Illuminate\Support\Collection {#5985
all: [
"Test User",
],
}
>
paginate()
メソッド)
・1ページあたりの件数を指定してテーブルからデータを取得する(・例として5件ずつデータを取得した場合
$paginatedUsers = App\Models\User::paginate(5);
以上です
Discussion