# Laravel tinker 使って DB データベース接続とコマンド

php artisan tinker

Laravel tinker は Laravel フレームワークに標準搭載されている対話式シェルです。
GUI ツール使わずに DB アクセスしたら、Laravel の機能を試せたりできます。

REPL とは

REPL (Read-Eval-Print Loop) は、入力・評価・出力のループのことです。

# 使い方

Laravel tinker の使い方は簡単です。

# query を使う

DB::select('select * from users limit 1;');
// => [
//      {
//        +"id": 1,
//        +"name": "laravel tinker",
//        +"email": "laravel_tinker@laravel.com",
//        +"password": null,
//        +"created_at": "2022-10-01 08:48:30",
//        +"updated_at": "2022-10-01 08:48:30",
//      },
//    ]

# DB 基本設定確認

config('database.default')
// => "mysql"

database 接続情報確認

config('database.default');
// => [
//      "driver" => "mysql",
//      "url" => null,
//      "host" => "host.amazonaws.com",
//      "port" => "3306",
//      "database" => "database",
//      "username" => "username",
//      "password" => "password",
//      "unix_socket" => "",
//      "charset" => "utf8mb4",
//      "collation" => "utf8mb4_unicode_ci",
//      "prefix" => "",
//      "prefix_indexes" => true,
//      "strict" => true,
//      "engine" => null,
//      "options" => [],
//    ]

DB::connection()->getConfig();
// => [
//      "driver" => "mysql",
//      "host" => "host.amazonaws.com",
//      "port" => "3306",
//      "database" => "database",
//      "username" => "username",
//      "password" => "password",
//      "unix_socket" => "",
//      "charset" => "utf8mb4",
//      "collation" => "utf8mb4_unicode_ci",
//      "prefix" => "",
//      "prefix_indexes" => true,
//      "strict" => true,
//      "engine" => null,
//      "options" => [],
//      "name" => "mysql",
//    ]

DB::connection()->getPdo();
// => PDO {
//      inTransaction: false,
//      attributes: {
//        CASE: NATURAL,
//        ERRMODE: EXCEPTION,
//        AUTOCOMMIT: 1,
//        PERSISTENT: false,
//        DRIVER_NAME: "mysql",
//        SERVER_INFO: "Uptime: 7034191  Threads: 3  Questions: 5001841  Slow queries: 0  Opens: 999  Flush tables: 3  Open
//  tables: 999  Queries per second avg: 0.711",
//        ORACLE_NULLS: NATURAL,
//        CLIENT_VERSION: "mysqlnd 8.0.19",
//        SERVER_VERSION: "8.0.23",
//        STATEMENT_CLASS: [
//          "PDOStatement",
//        ],
//        EMULATE_PREPARES: 0,
//        CONNECTION_STATUS: "host.amazonaws.com via TCP/IP",
//        DEFAULT_FETCH_MODE: BOTH,
//      },
//    }

# help コマンド

tinker で使えるコマンド一覧

ls コマンド(変数の表示)
ls コマンドはローカル、インスタンス、またはクラスの変数、メソッド、定数をリスト表示します。

>>> help
  help             Show a list of commands. Type `help [foo]` for information about [f
  ls               List local, instance or class variables, methods and constants.
  dump             Dump an object or primitive.
  doc              Read the documentation for an object, class, constant, method or pr
  show             Show the code for an object, class, constant, method or property.
  wtf              Show the backtrace of the most recent 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.
  clear            Clear the Psy Shell screen.
  edit             Open an external editor. Afterwards, get produced code in input buf
  sudo             Evaluate PHP code, bypassing visibility restrictions.
  history          Show the Psy Shell history.
  exit             End the current session and return to caller.
  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
2022-06-16
  • php
  • laravel

関連記事

正規表現一覧 よく使う検索・置換のパターン
Carbon で php date 日付の日数・月数差を計算
nuxtjs と codeigniter で jwt システム構築
Apache 初期設定メモ
Codeigniter APPPATH BASEPATH FCPATH 各種パスと URL 取得
開発におけるコーディングルール・規約
Codeigniter 画像アップロードとリサイズ
爆速軽量フレームワーク codeigniter PHP 開発
Composer コマンドとオプション
codeigniter email ライブラリでメール送信 日本語対応
php CSV データ取得は fgetcsv 使う
php curl 使って クリックなしで POST 送信
PHP empty isset is_null の違い
PHP mbconvertkana 全角半角英数カナ変換
FlattenException deprecated
allowurlinclude の設定で ftp_connect()エラー
php Exception エラーキャッチでメール送信
Class 'Imagick' not found Error
Laravel blade foreach loop と current url
Laravel eloquent model の規約
Laravel でカテゴリー作成 テーブル構築と再帰クエリ
Laravel Email バリデーションについて
Laravel Error についてのメモ
Laravel lang バリデーションメッセージを多言語対応
Laravel Log の基本設定&使い方
Laravel Lumen Faker 日本語設定
Laravel logger でエラーログを chatwork に自動送信
Lumen と Laravel 違い比較
Laravel メンテナンスモード
laravel method の基本 get post put options
Laravel model で hidden に設定したカラムを一時解除
Laravel を API サーバーとしての初期設定
Laravel Queue で非同期処理
Laravel リクエストログ出力
Laravel Sail で Docker 環境構築
Laravel Sanctum 使って API トークン JWT 認証と SPA 認証
Laravel notification メール通知カスタマイズ
laravel session を制する
Laravel Test についてのメモ
Laravel schedule 設定
Laravel timestamp() auto update 有効化無効化
Laravel toSql パラメータ付きで出力
Laravel 5.1 から 8.1 にバージョンアップ
Laravel 429 Too Many Requests
laravel に vuejs 使うための初期設定
Lumen8 で JWT ユーザー認証
Lumen8 で API 開発
PHP 7.4 にアップグレードして使えなくなる機能
HTML から PDF に変換 PHP ライブラリ mPDF の設定
PHP 8 リリース新機能と変更
PHP 8.1 から 8.4 へアップグレードしました
php.ini 初期設定のいろいろ
Smarty HTTP URL 取得できるサーバー関数
解決!phpMyAdmin テーブル構造の内容が表示されない問題
PHP 文字列長さ・文字列の幅を取得方法
twig 3 人気 PHP テンプレートエンジンがバージョンアップ
開発時によく使うゼロ埋めパディング作業まとめ
Exception: Class 'ZipArchive' not found
AWS SES メール開封確認  DB に集計