Home >PHP Framework >Laravel >Introduction to some common model properties in Laravel
This article will introduce you to some commonly used model attributes in Laravel. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
$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
/** * 如果使用的是非递增或者非数字的主键。 * * @var bool */ public $incrementing = false;
$with
class Post extends Model { /** * 加载模型关联数据。 * * @var array */ protected $with = [ 'comments' ]; }
$withCount
class Post extends Model { /** * 加载模型关联数据数量。 * * @var array */ protected $withCount = [ 'comments' ]; }
$timestamps
/** * 执行模型是否自动维护时间戳. * * @var bool */ public $timestamps = false;
Note: guarded and fillable can only be used in the current model There is one.
$fillable
/** * 可以被批量赋值的属性。 * * @var array */ protected $fillable = ['name', 'age'];
$guarded
/** * 不可被批量赋值的属性,当 $guarded 为空数组时则所有属性都可以被批量赋值。 * * @var array */ protected $guarded = ['price'];
CREATED_AT
/** * 创建时间戳字段名称。 * * @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';
I don’t know what U means, please see Date/Time function.
$appends
/** * 追加到模型数组表单的访问器。 * * @var array */ protected $appends = ['is_admin'];
Generally appends are used with accessor.
$hidden
/** * 数组中的属性会被隐藏。 * * @var array */ protected $hidden = ['password'];
$visible
/** * 数组中的属性会被展示。 * * @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
/** * 默认分页数量。 * * @var int */ protected $perPage = 50;
$touches
/** * 更新添加的关联模型的 updated_at 字段。 * * @var array */ protected $touches = ['post'];
For more programming-related knowledge, please visit: Introduction to Programming! !
The above is the detailed content of Introduction to some common model properties in Laravel. For more information, please follow other related articles on the PHP Chinese website!