溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Laravel中常用模型屬性有哪些

發(fā)布時間:2021-03-02 10:45:50 來源:億速云 閱讀:231 作者:清風 欄目:編程語言

這篇“Laravel中常用模型屬性有哪些”文章,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要參考一下,對于“Laravel中常用模型屬性有哪些”,小編整理了以下知識點,請大家跟著小編的步伐一步一步的慢慢理解,接下來就讓我們進入主題吧。

Laravel 是什么

Laravel 是一套簡潔、優(yōu)雅的PHP Web開發(fā)框架。它可以讓你從面條一樣雜亂的代碼中解脫出來;它可以幫你構建一個完美的網(wǎng)絡APP,而且每行代碼都可以簡潔、富于表達力。

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
{
 /**
  * 加載模型關聯(lián)數(shù)據(jù)。
  * 
  * @var array
  */
  protected $with = [
      'comments'
  ];
}

$withCount

class Post extends Model
{
 /**
  * 加載模型關聯(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

 /**
  * 字段轉換為對應的類型。
  *
  * @var array
  */
 protected $casts = [
    'id' => 'integer',
    'settings' => 'array',
    'is_admin' => 'boolean',
 ];

$dates

 /**
  * 需要轉換成日期的屬性。
  *
  * @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 都是與 訪問器 連用的。

$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

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

以上是“Laravel中常用模型屬性有哪些”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI