# 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

関連記事

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