本篇文章給大家介紹一些Laravel中常用模型屬性。有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)大家有所幫助。
$connection
/** * 為模型指定一個(gè)連接名稱(chēng)。 * * @var string */ protected $connection = 'connection-name';
$table
/** * 為模型指定一個(gè)表名。 * * @var string */ protected $table = 'users';
$primaryKey
/** * 為模型指定主鍵。 * * @var string */ protected $primaryKey = 'user_id';
$keyType
/** * 自定義主鍵類(lèi)型。 * * @var string */ protected $keyType = 'string';
$incrementing
/** * 如果使用的是非遞增或者非數(shù)字的主鍵。 * * @var bool */ public $incrementing = false;
$with
class Post extends Model { /** * 加載模型關(guān)聯(lián)數(shù)據(jù)。 * * @var array */ protected $with = [ 'comments' ]; }
$withCount
class Post extends Model { /** * 加載模型關(guān)聯(lián)數(shù)據(jù)數(shù)量。 * * @var array */ protected $withCount = [ 'comments' ]; }
$timestamps
/** * 執(zhí)行模型是否自動(dòng)維護(hù)時(shí)間戳. * * @var bool */ public $timestamps = false;
注:guarded 與 fillable,在當(dāng)前模型中只能存在一者噢。
$fillable
/** * 可以被批量賦值的屬性。 * * @var array */ protected $fillable = ['name', 'age'];
$guarded
/** * 不可被批量賦值的屬性,當(dāng) $guarded 為空數(shù)組時(shí)則所有屬性都可以被批量賦值。 * * @var array */ protected $guarded = ['price'];
CREATED_AT
/** * 創(chuàng)建時(shí)間戳字段名稱(chēng)。 * * @var string */ const CREATED_AT = 'created_at';
UPDATED_AT
/** * 更新時(shí)間戳字段名稱(chēng)。 * * @var string */ const UPDATED_AT = 'updated_at';
$attributes
const STATUS_CREATED = 'created'; /** * 給定字段默認(rèn)值。 * * @var array */ protected $attributes = [ 'status' => self::STATUS_CREATED, ];
$casts
/** * 字段轉(zhuǎn)換為對(duì)應(yīng)的類(lèi)型。 * * @var array */ protected $casts = [ 'id' => 'integer', 'settings' => 'array', 'is_admin' => 'boolean', ];
$dates
/** * 需要轉(zhuǎn)換成日期的屬性。 * * @var array */ protected $dates = ['deleted_at'];
$dateFormat
/** * 模型中日期字段的保存格式。 * * @var string */ protected $dateFormat = 'U';
不清楚 U 是什么意思的,請(qǐng)看 Date/Time 函數(shù) 。
$appends
/** * 追加到模型數(shù)組表單的訪(fǎng)問(wèn)器。 * * @var array */ protected $appends = ['is_admin'];
一般情況下 appends 都是與 訪(fǎng)問(wèn)器 連用的。
$hidden
/** * 數(shù)組中的屬性會(huì)被隱藏。 * * @var array */ protected $hidden = ['password'];
$visible
/** * 數(shù)組中的屬性會(huì)被展示。 * * @var array */ protected $visible = ['first_name', 'last_name'];
$dispatchesEvents
/** * 模型的事件映射。 * * @var array */ protected $dispatchesEvents = [ 'saved' => UserSaved::class, 'deleted' => UserDeleted::class, ];
$forceDeleting
/** * 指示模型當(dāng)前是否強(qiáng)制刪除。 * * @var bool */ protected $forceDeleting = false;
$perPage
/** * 默認(rèn)分頁(yè)數(shù)量。 * * @var int */ protected $perPage = 50;
$touches
/** * 更新添加的關(guān)聯(lián)模型的 updated_at 字段。 * * @var array */ protected $touches = ['post'];
更多編程相關(guān)知識(shí),請(qǐng)?jiān)L問(wèn):編程入門(mén)!!
當(dāng)前名稱(chēng):Laravel中的一些常用模型屬性介紹
網(wǎng)頁(yè)路徑:http://weahome.cn/article/chdsjj.html