tinkerの起動
php artisan tinker
でtinkerを起動。
1 2 3 |
[vagrant@localhost laravel_tutorial]$ php artisan tinker Psy Shell v0.10.8 (PHP 7.3.29 — cli) by Justin Hileman >>> |
help
でコマンドが確認できる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
>>> 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, 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 clear-compiled Remove the compiled class file down Put the application into maintenance mode env Display the current framework environment optimize Cache the framework bootstrap files up Bring the application out of maintenance mode migrate Run the database migrations inspire Display an inspiring quote |
モデル・データベースの操作
データの登録
モデルとマイグレーションで作成したモデルを使う。
まずshow
コマンドでモデルクラスを見てみる。
1 2 3 4 5 6 |
>>> show TestModel [!] Aliasing 'TestModel' to 'App\TestModel' for this Tinker session. 7: class TestModel extends Model 8: { 9: // 10: } |
変数$model
にTestModel
クラスのインスタンスを生成。
1 2 |
>>> $model = new App\TestModel(); => App\TestModel {#3370} |
マイグレーションファイルで定義したフィールド(product_name
、amount
)に値を代入。
1 2 3 4 |
>>> $model->product_name = 'wire'; => "wire" >>> $model->amount = 100; => 10 |
変数を入力すると、その内容が確認できる。
1 2 3 4 5 |
>>> $model; => App\TestModel {#3370 product_name: "wire", amount: 100, } |
ls
で定義された変数を、ls -l
でそれらを少し詳しく見ることができる。
1 2 3 4 5 6 |
>>> ls Variables: $model >>> ls -l Variables: $model App\TestModel {#3370 …5} |
変数の内容をデータベースに保存するステートメントを実行。
1 2 |
>>> $model->save(); => true |
データベース側で確認すると、日付もちゃんと登録されている。
1 2 3 4 5 6 7 |
mysql> SELECT * FROM test_models; +----+--------------+--------+---------------------+---------------------+ | id | product_name | amount | created_at | updated_at | +----+--------------+--------+---------------------+---------------------+ | 1 | wire | 100 | 2021-08-23 14:33:27 | 2021-08-23 14:33:27 | +----+--------------+--------+---------------------+---------------------+ 1 row in set (0.00 sec) |
さらに2つのデータを登録。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
>>> $model->product_name = 'steel plate'; => "steel plate" >>> $model->amount = 50; => 50 >>> $model->save(); => true >>> $model = new App\TestModel(); => App\TestModel {#4110} >>> $model->product_name = 'screws'; => "screws" >>> $model->amount = 150; => 150 >>> $model->save(); => true |
データベース側でも登録されているのが確認できる。
1 2 3 4 5 6 7 8 9 |
mysql> SELECT * FROM test_models; +----+--------------+--------+---------------------+---------------------+ | id | product_name | amount | created_at | updated_at | +----+--------------+--------+---------------------+---------------------+ | 1 | wire | 100 | 2021-08-23 14:33:27 | 2021-08-23 14:33:27 | | 2 | steel plate | 50 | 2021-08-23 14:51:02 | 2021-08-23 14:51:02 | | 3 | screws | 150 | 2021-08-23 14:53:44 | 2021-08-23 14:53:44 | +----+--------------+--------+---------------------+---------------------+ 3 rows in set (0.00 sec) |
全データ取得
tinker
で変数$models
にデータベースの全データを取得。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
>>> $models = App\TestModel::all(); => Illuminate\Database\Eloquent\Collection {#4327 all: [ App\TestModel {#4328 id: 1, product_name: "wire", amount: 100, created_at: "2021-08-23 14:33:27", updated_at: "2021-08-23 14:33:27", }, App\TestModel {#4329 id: 2, product_name: "steel plate", amount: 50, created_at: "2021-08-23 14:51:02", updated_at: "2021-08-23 14:52:53", }, App\TestModel {#4330 id: 3, product_name: "screws", amount: 150, created_at: "2021-08-23 14:53:44", updated_at: "2021-08-23 14:53:44", }, ], } |
$models
は配列で要素はゼロから始まるので、$models[1]
は2番目のデータ。
1 2 3 4 5 6 7 8 |
>>> $models[1]; => App\TestModel {#4329 id: 2, product_name: "steel plate", amount: 50, created_at: "2021-08-23 14:51:02", updated_at: "2021-08-23 14:51:02", } |
データの更新
スタティック関数find()
でid=2
のデータを$model
に取得。
1 2 3 4 5 6 7 8 |
>>> $model = App\TestModel::find(2); => App\TestModel {#4073 id: 2, product_name: "steel plate", amount: 50, created_at: "2021-08-23 14:51:02", updated_at: "2021-08-23 14:52:53", } |
取得したid=2
のデータのamount
を0
に変更して保存。
1 2 3 4 |
>>> $model->amount = 0; => 0 >>> $model->save(); => true |
データが更新されているのを確認。
1 2 3 4 5 6 7 8 |
>>> App\TestModel::find(2); => App\TestModel {#4332 id: 2, product_name: "steel plate", amount: 0, created_at: "2021-08-23 14:51:02", updated_at: "2021-08-23 15:01:36", } |
データベースでも確認できる。
1 2 3 4 5 6 7 8 9 |
mysql> SELECT * FROM test_models; +----+--------------+--------+---------------------+---------------------+ | id | product_name | amount | created_at | updated_at | +----+--------------+--------+---------------------+---------------------+ | 1 | wire | 100 | 2021-08-23 14:33:27 | 2021-08-23 14:33:27 | | 2 | steel plate | 0 | 2021-08-23 14:51:02 | 2021-08-23 15:01:36 | | 3 | screws | 150 | 2021-08-23 14:53:44 | 2021-08-23 14:53:44 | +----+--------------+--------+---------------------+---------------------+ 3 rows in set (0.00 sec) |
データの削除
id=2
のデータを取得。
1 2 3 4 5 6 7 8 |
>>> $model = App\TestModel::find(2); => App\TestModel {#4267 id: 2, product_name: "steel plate", amount: 0, created_at: "2021-08-23 14:51:02", updated_at: "2021-08-23 15:01:36", } |
delete()
メソッドでデータベースから削除。
1 2 |
>>> $model->delete(); => true |
全データを取得すると、id=2
のデータが削除されている。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
>>> App\TestModel::all(); => Illuminate\Database\Eloquent\Collection {#4336 all: [ App\TestModel {#4338 id: 1, product_name: "wire", amount: 100, created_at: "2021-08-23 14:33:27", updated_at: "2021-08-23 14:33:27", }, App\TestModel {#4339 id: 3, product_name: "screws", amount: 100, created_at: "2021-08-23 14:53:44", updated_at: "2021-08-23 14:58:51", }, ], } |
データベース側でも確認できる。
1 2 3 4 5 6 7 8 |
mysql> SELECT * FROM test_models; +----+--------------+--------+---------------------+---------------------+ | id | product_name | amount | created_at | updated_at | +----+--------------+--------+---------------------+---------------------+ | 1 | wire | 100 | 2021-08-23 14:33:27 | 2021-08-23 14:33:27 | | 3 | screws | 150 | 2021-08-23 14:53:44 | 2021-08-23 14:53:44 | +----+--------------+--------+---------------------+---------------------+ 2 rows in set (0.00 sec) |