LoginSignup
2
2

More than 3 years have passed since last update.

【Mac】Laravel環境 artisan tinkerでDBアクセス ついでにpsyshのインストールからHelloWorld

Last updated at Posted at 2019-06-21

Laravel環境でtinkerを利用してPHPを実行しDBアクセスする

tinkerとは?

  • Replライブラリ、インタラクティブ(対話)シェルの一つで、artisanのオプションの一つ
  • rubyでいうirbみたいな事ができる
  • デバッグとか色々できるらしい

実行手順

  • DBアクセスしたいので、DB起動(この例ではPostgreSQL)
➜ pg_ctl -D /usr/local/var/postgres/xxxx -l logfile start
waiting for server to start.... done
server started
  • artisan の tinker起動
➜ php artisan tinker

Psy Shell v0.9.9 (PHP 7.2.19 — cli) by Justin Hileman
>>>
  • とりあえず何かしらのテーブルのデータを取得
    例)publisherテーブルのレコード全件取得
      publishsherモデルクラスは作成済みであることを前提とする
>>> publisher::all()
[!] Aliasing 'publisher' to 'App\publisher' for this Tinker session.
=> Illuminate\Database\Eloquent\Collection {#2940
     all: [
       App\publisher {#2941
         id: 1,
         publisher_name: "aaaaaaaa",
         fax_number: "0311112222",
         tel_number: "0355556666",
       },
       App\publisher {#2942
         id: 2,
         publisher_name: "bbbbbbbb",
         fax_number: "0377779999",
         tel_number: "0388889999",
       },
       App\publisher {#2943
         id: 3,
         publisher_name: "cccccccc",
         fax_number: "0344445555",
         tel_number: "0355554444",
       },
     ],
   }
>>> 

tinkerの終了

➜ q

または

➜ quit

または

➜ exit

素のphpのReplならpsysh

psyshとは?

  • rubyでいうirbみたいな事ができる
  • Replライブラリ、インタラクティブ(対話)シェル って言うらしい
  • 他にborisってのがあるが、psyshの方が便利らしい
  • デバッグもできる
  • 参考サイト

インストール

  • 下記をコマンドラインから実行し、インストール
composer g require psy/psysh:@stable
  • パスの追加(どこからでも実行できるようにする)
echo 'export PATH="$HOME/.composer/vendor/bin:$PATH"' >> ~/.zshrc
  • 下記のようになれば、インストール成功
➜ psysh

Psy Shell v0.9.9 (PHP 7.2.19 — cli) by Justin Hileman
>>>

hello world

>>> $temp = "hello world"
=> "hello world"
>>>

psyshの終了

➜ q

または

➜ quit

または

➜ exit
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2