# 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

同じタグを持つ記事をピックアップしました。