1
0

Laravel開発をちょっと快適に! tinkerの使い方

Last updated at Posted at 2024-02-15

はじめに

Laravelでクエリビルダを調べてる時に気になったので深掘りしてみました。

tinkerとは

Laravelに標準で搭載されている対話型のコンソール。

LaravelでクエリビルダやEloquentの結果を確認する事ができるみたいです。

取得したデータを確認したい時に便利かなと思いました。

とりあえず使ってみた

使い方

下記コマンド入力
php artisan tinker 

kamimurayuuta@uemurayuutanoMacBook-Pro todoTask % php artisan tinker

Psy Shell v0.11.12 (PHP 8.1.10 — cli) by Justin Hileman
> 

モデルの取得(全件)

> App\Models\User::all()
= Illuminate\Database\Eloquent\Collection {#4466
   all: [
     App\Models\User {#4720
       id: 1,
       name: "test",
       email: "dummy@email.com",
       email_verified_at: null,
       #password: "$2y$10$0WmCBESuQUzgnRQQhisAo./Rvo/baZ4f.sO7ByObNZXp/6pITgtYG",
       #remember_token: null,
       created_at: "2023-03-09 06:22:28",
       updated_at: "2023-03-09 06:22:28",
     },
     App\Models\User {#4721
       id: 2,

モデルの取得(ID指定)

> App\Models\User::find(2)
= App\Models\User {#4103
   id: 2,
   name: "テストユーザー",
   email: "test@test.com",
   email_verified_at: null,
   #password: "$2y$10$6l59SE2dTBx53362n.TysuQJcoAwewdcEQkWCL6n.uabrbPyuV9MW",
   #remember_token: null,
   created_at: "2023-03-09 06:56:14",
   updated_at: "2023-03-09 06:56:14",
 }

サクッとテーブルの中身が見れそうです!

Carbonで日付確認

> Carbon\CarbonImmutable::now()
= Carbon\CarbonImmutable @1678444016 {#4465
    date: 2023-03-10 10:26:56.803323 UTC (+00:00),
  }

ライブラリの中身も見れます!
初めて使うライブラリとかで役に立ちそうです。

historyで過去のコマンド確認

> history
 0: App\Models\User::all();
 1: exit
 2: help
 3: history
 4: $user = User::find(1)
 5: $user->name
 6: $event
 7: $event->editEventDate
 8: Carbon::today()
 9: echo('hello');
10: $hello = 'world';
11: Carbon\Carbon::now();
12: history
13: User::where('id', 1)->get();

コマンドの履歴も確認する事ができます!

返り値の確認

<?php

namespace App\Utils;

class Hello
{
    public static function sayHello()
    {
        return 'hello';
    }
}
>>> App\Utils\Hello::sayHello();
=> "hello"

メソッドの返り値も確認できます。
返り値が確認できるのは便利だと思います。

ddでしかデバックしてなかったので、デバックの幅が広がりそうです。

1
0
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
1
0