2 回答

TA貢獻1830條經驗 獲得超3個贊
決定如何在 Tinker 中顯示模型屬性的代碼位于 Tinker 代碼中,而不是模型中:
Casters 是Symfony VarDumper 組件的一部分,?PsySH使用該組件來顯示類。Tinker 是建立在 PsySH 之上的。
Tinker 將腳輪映射到其 Console 命令類中的類:
這將返回類到其施法者的映射:
/**
? ? ?* Get an array of Laravel tailored casters.
? ? ?*
? ? ?* @return array
? ? ?*/
? ? protected function getCasters()
? ? {
? ? ? ? $casters = [
? ? ? ? ? ? 'Illuminate\Support\Collection' => 'Laravel\Tinker\TinkerCaster::castCollection',
? ? ? ? ? ? 'Illuminate\Support\HtmlString' => 'Laravel\Tinker\TinkerCaster::castHtmlString',
? ? ? ? ? ? 'Illuminate\Support\Stringable' => 'Laravel\Tinker\TinkerCaster::castStringable',
? ? ? ? ];
? ? ? ? if (class_exists('Illuminate\Database\Eloquent\Model')) {
? ? ? ? ? ? $casters['Illuminate\Database\Eloquent\Model'] = 'Laravel\Tinker\TinkerCaster::castModel';
? ? ? ? }
? ? ? ? if (class_exists('Illuminate\Foundation\Application')) {
? ? ? ? ? ? $casters['Illuminate\Foundation\Application'] = 'Laravel\Tinker\TinkerCaster::castApplication';
? ? ? ? }
? ? ? ? return $casters;
? ? }
這將設置 VarDumper 上的腳輪:
? ? ? ? $config->getPresenter()->addCasters(
? ? ? ? ? ? $this->getCasters()
? ? ? ? );
如果要從模型中添加要在 Tinker 中顯示的其他屬性,可以在模型上使用 $ appends屬性:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
? ? /**
? ? ?* The accessors to append to the model's array form.
? ? ?*
? ? ?* @var array
? ? ?*/
? ? protected $appends = ['is_admin'];
}
- 2 回答
- 0 關注
- 119 瀏覽
添加回答
舉報