Files
cunkebao_v3/Moncter/app/model/User.php
2025-11-07 15:40:33 +08:00

37 lines
1.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// app/model/User.php
namespace app\model;
use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Relations\HasMany; // 若需关联查询(可选)
class User extends Model
{
// 对应 MongoDB 集合名(默认复数,可自定义)
protected $collection = 'users';
// 主键MongoDB 默认 _id无需修改自动转为字符串
protected $primaryKey = '_id';
// 主键类型(官方推荐显式声明)
protected $keyType = 'string';
// 允许批量赋值的字段(白名单)
protected $fillable = ['name', 'age', 'email', 'avatar'];
// 自动转换字段类型ObjectId 转字符串、日期转 Carbon
protected $casts = [
'_id' => 'string',
'age' => 'integer',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'tags' => 'array', // 支持数组类型MongoDB 原生支持数组)
];
// 自动维护时间戳created_at/updated_at默认启用
// 若不需要可关闭public $timestamps = false;
// 自定义时间戳字段名(可选)
// const CREATED_AT = 'create_time';
// const UPDATED_AT = 'update_time';
}