Laravel中的一些常用模型屬性介紹

本篇文章給大家介紹一些laravel中常用模型屬性。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。

Laravel中的一些常用模型屬性介紹

$connection

 /**   * 為模型指定一個連接名稱。   *   * @var string   */  protected $connection = 'connection-name';

$table

/**  * 為模型指定一個表名。  *  * @var string  */  protected $table = 'users';

$primaryKey

/**  * 為模型指定主鍵。  *  * @var string  */  protected $primaryKey = 'user_id';

$keyType

 /**   * 自定義主鍵類型。   *   * @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í)行模型是否自動維護時間戳.   *   * @var bool   */  public $timestamps = false;

注:guarded 與 fillable,在當前模型中只能存在一者噢。

$fillable

/**  * 可以被批量賦值的屬性。  *  * @var array  */  protected $fillable = ['name', 'age'];

$guarded

 /**   * 不可被批量賦值的屬性,當 $guarded 為空數(shù)組時則所有屬性都可以被批量賦值。   *   * @var array   */  protected $guarded = ['price'];

CREATED_AT

 /**   * 創(chuàng)建時間戳字段名稱。   *   * @var string   */  const CREATED_AT = 'created_at';

UPdateD_AT

 /**   * 更新時間戳字段名稱。   *   * @var string   */  const UPDATED_AT = 'updated_at';

$attributes

 const STATUS_CREATED = 'created';   /**   * 給定字段默認值。   *   * @var array   */  protected $attributes = [      'status' => self::STATUS_CREATED,  ];

$casts

 /**   * 字段轉(zhuǎn)換為對應(yīng)的類型。   *   * @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?是什么意思的,請看?Date/Time 函數(shù)?。

$appends

 /**   * 追加到模型數(shù)組表單的訪問器。   *   * @var array   */  protected $appends = ['is_admin'];

一般情況下?appends?都是與?Date/Time 函數(shù) 連用的。

$hidden

 /**   * 數(shù)組中的屬性會被隱藏。   *   * @var array   */  protected $hidden = ['password'];

$visible

 /**   * 數(shù)組中的屬性會被展示。   *   * @var array   */  protected $visible = ['first_name', 'last_name'];

$dispatchesEvents

 /**   * 模型的事件映射。   *   * @var array   */  protected $dispatchesEvents = [      'saved' => UserSaved::class,      'deleted' => UserDeleted::class,  ];

$forceDeleting

 /**   * 指示模型當前是否強制刪除。   *   * @var bool   */  protected $forceDeleting = false;

$perPage

 /**   * 默認分頁數(shù)量。   *   * @var int   */  protected $perPage = 50;

$touches

/**   * 更新添加的關(guān)聯(lián)模型的 updated_at 字段。   *   * @var array   */  protected $touches = ['post'];

更多編程相關(guān)知識,請訪問:Date/Time 函數(shù)!!

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊7 分享