使用 Tinx 過載 Laravel Tinker 會話

BradStevens發表於2017-10-26

Laravel TinxAnders Jürisoo 寫的一個新包,Tinx 可以從 tinker 內部過載一個 Laravel Tinker 命令列,同時為first() , find()search() 這樣的操作提供了易理解的縮略寫法。
安裝包之後,你可以通過一個 artisan 命令來使用它:

php artisan tinx

Tinx 一個突出功能特性是通過 re() 命令來更新你的 Tinker 會話。

$ php artisan tinx
Tinx - something awesome is about to happen.

>>> re()
Reloading your tinker session.
>>>

Tinx 已有一些快捷鍵,亦會探查你的模型檔案來配備一些簡略方法。下面是縮略方法 ($u) 來獲取第一個使用者 (在你至少建立一個使用者之後):

$ php artisan tinx

>>> factory('App\User', 10)->create();
=> App\User {#959
         name: "Florine Herzog",
         email: "schowalter.rowan@example.net",
         updated_at: "2017-10-24 03:10:11",
         created_at: "2017-10-24 03:10:11",
         id: 1,
     }
     ...
>>> $u
=> {#919
         +"name": "example",
         +"email": "you@example.com",
     }

如果你想通過 id 來獲取特定的使用者,你可以將 id 傳參到 u() 方法來獲取。

$ php artisan tinx
>>> u()
=> "App\User"

>>> u(3)
=> App\User {#773
         id: 3,
         name: "Alicia Grant",
         email: "veda.koepp@example.net",
         created_at: "2017-10-24 03:13:47",
         updated_at: "2017-10-24 03:13:47",
     }

下面是一些 readme 上寫的簡略方法工作原理的例子:

用法示例      等同

$u       App\User::first()

$c      App\Models\Car::first()

u(3)      App\User::find(3)

u("gmail")   Where "gmail" is found in any column.

u()      "App\User"

u()::where(...)   App\User::where(...)

如果你有多個相同字母開頭的模型檔案,例如 PicturePlayers. Tinx 會將 $pi$pl 作為對應模型的第一條記錄,PicturePlayers 兼帶的助手函式為 pi()pl().
訪問官方倉庫來獲取更多資訊及從 Laravel Tinker 會話中獲得一些驚豔的額外功能。

原文連結:https://laravel-news.com/laravel-tinx
如果翻譯的不好,請指正,我再修改

Keep Young, Keep Simple.

相關文章