Home > PHP Framework > Laravel > body text

Introduction to some common model properties in Laravel

PHPz
Release: 2020-09-27 10:20:58
forward
4078 people have browsed it

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.

Introduction to some common model properties in Laravel

$connection

 /**
  * 为模型指定一个连接名称。
  *
  * @var string
  */
 protected $connection = 'connection-name';
Copy after login

$table

/**
 * 为模型指定一个表名。
 *
 * @var string
 */
 protected $table = 'users';
Copy after login

$primaryKey

/**
 * 为模型指定主键。
 *
 * @var string
 */
 protected $primaryKey = 'user_id';
Copy after login

$keyType

 /**
  * 自定义主键类型。
  *
  * @var string
  */
 protected $keyType = 'string';
Copy after login

$incrementing

 /**
  * 如果使用的是非递增或者非数字的主键。
  *
  * @var bool
  */
 public $incrementing = false;
Copy after login

$with

class Post extends Model
{
 /**
  * 加载模型关联数据。
  * 
  * @var array
  */
  protected $with = [
      'comments'
  ];
}
Copy after login

$withCount

class Post extends Model
{
 /**
  * 加载模型关联数据数量。
  * 
  * @var array
  */
  protected $withCount = [
      'comments'
  ];
}
Copy after login

$timestamps

 /**
  * 执行模型是否自动维护时间戳.
  *
  * @var bool
  */
 public $timestamps = false;
Copy after login

Note: guarded and fillable can only be used in the current model There is one.

$fillable

/**
 * 可以被批量赋值的属性。
 *
 * @var array
 */
 protected $fillable = ['name', 'age'];
Copy after login

$guarded

 /**
  * 不可被批量赋值的属性,当 $guarded 为空数组时则所有属性都可以被批量赋值。
  *
  * @var array
  */
 protected $guarded = ['price'];
Copy after login

CREATED_AT

 /**
  * 创建时间戳字段名称。
  *
  * @var string
  */
 const CREATED_AT = 'created_at';
Copy after login

UPDATED_AT

 /**
  * 更新时间戳字段名称。
  *
  * @var string
  */
 const UPDATED_AT = 'updated_at';
Copy after login

$attributes

 const STATUS_CREATED = 'created';

 /**
  * 给定字段默认值。
  *
  * @var array
  */
 protected $attributes = [
     'status' => self::STATUS_CREATED,
 ];
Copy after login

$casts

 /**
  * 字段转换为对应的类型。
  *
  * @var array
  */
 protected $casts = [
    'id' => 'integer',
    'settings' => 'array',
    'is_admin' => 'boolean',
 ];
Copy after login

$dates

 /**
  * 需要转换成日期的属性。
  *
  * @var array
  */
 protected $dates = ['deleted_at'];
Copy after login

$dateFormat

 /**
  * 模型中日期字段的保存格式。
  *
  * @var string
  */
 protected $dateFormat = 'U';
Copy after login

I don’t know what U means, please see Date/Time function.

$appends

 /**
  * 追加到模型数组表单的访问器。
  *
  * @var array
  */
 protected $appends = ['is_admin'];
Copy after login

Generally appends are used with accessor.

$hidden

 /**
  * 数组中的属性会被隐藏。
  *
  * @var array
  */
 protected $hidden = ['password'];
Copy after login

$visible

 /**
  * 数组中的属性会被展示。
  *
  * @var array
  */
 protected $visible = ['first_name', 'last_name'];
Copy after login

$dispatchesEvents

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

$forceDeleting

 /**
  * 指示模型当前是否强制删除。
  *
  * @var bool
  */
 protected $forceDeleting = false;
Copy after login

$perPage

 /**
  * 默认分页数量。
  *
  * @var int
  */
 protected $perPage = 50;
Copy after login

$touches

/**
  * 更新添加的关联模型的 updated_at 字段。
  *
  * @var array
  */
 protected $touches = ['post'];
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template