您好,欢迎访问一九零五行业门户网

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
 /**  * 如果使用的是非递增或者非数字的主键。  *  * @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;
注:guarded 与 fillable,在当前模型中只能存在一者噢。
$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';
不清楚 u 是什么意思的,请看 date/time 函数 。
$appends
 /**  * 追加到模型数组表单的访问器。  *  * @var array  */ protected $appends = ['is_admin'];
一般情况下 appends 都是与 访问器 连用的。
$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'];
更多编程相关知识,请访问:编程入门!!
以上就是laravel中的一些常用模型属性介绍的详细内容。
其它类似信息

推荐信息