# Data and Models
[[toc]]
# Introduction
Each model in Blessing Skin is associated with a data table in the database, and each model instance is equivalent to a row of data in the data table. All three models of Blessing Skin are Laravel Eloquent ORM models. If you want to get more information about Laravel Eloquent ORM, I recommend reading the Laravel documentation (opens new window) .
Currently there are these models in Blessing Skin: User
, Player
and
Texture
, which are all located under App\Models
. We will introduce
each of these models in detail, and at the end we will talk about the
connections between these models and how to use these connections to
simplify the code.
It's worth noting that all Laravel Eloquent ORM models inherit from
Illuminate\Database\Eloquent\Model
and thus have Eloquent ORM
features. Even if no properties are declared on the classes of these
models, you can still access data on the model instances. For example,
the User model has a ``score field
on the corresponding data table in
the database , but you do not see the attribute of the
score class in the ``App\Models\User class
. Even so, you can still
use it like $user->score
This way to get or set the value on the
score
field.
# basic operation
# Get model instance
If you want to find a model instance by primary key, you can call the
model's find
static method. For example, if you want to find a User
model instance with uid 1, you can ``:
$user = User ::find( 1 ) ;
null
will be returned .
If you want to select by condition, you can use the where
static
method. For example, if you want to find all users with points greater
than 50, you can:
$users = User ::find( 'score' , '>' , 50 )->get() ;
the Laravel docs (opens new window) for collections ). If no matching users are found, it will return an empty collection. Call the `isEmpty` method to determine whether it is an empty collection:
$users- >isEmpty() ;
If the comparison operator is "=" during the conditional query, the
second parameter can be omitted directly. For example, the following
finds users with email address a@bc
:
$user = User ::where( 'email' , 'a@bc' )->first() ;
Since the where
method always returns a collection, but email
addresses are unique in the data table, this collection often has only
one element. So in the above example, we called the first
method
instead of the get
method, which means getting the first element in
the collection.
# update model data
After getting a model instance, we can update the data on the model
instance. For example, we want to change a user's email address to
a@bc
:
$user- >email = 'a@bc' ;
$user- >save() ;
The last save
method is to save the changes to the database.
# add model
If you want to add a model, you only need to construct a model instance, then modify the data, and finally save it. However, the following example is only for demonstration, and may not work in practice, because we have not written all the field information. Note that for the auto-incremented primary key, we don't need to fill it in manually.
// create a new user model instance
$user = new User () ;
$user- > email = 'a@bc' ;
$user- >nickname = 'abc' ;
$user- >save() ;
# User
model
User
model represents the user, and the user's data is recorded on the
model instance. The primary key of the User
model in the data table is
uid
, which is a self-increasing integer field, so you cannot modify a
user's uid
because it is a unique identifier.
The following will introduce the meaning of each data attribute on the
User model.
# uid
This represents the unique identifier of each user, and you cannot
modify the value of this attribute. At the same time, because it is a
unique identifier, one uid
corresponds to one user.
# email
This represents the user's email address. This property can be modified.
The user's email address is also unique in the data table, but it cannot
replace the uid
.
# nickname
This represents the user's nickname. This property can be modified.
Generally speaking, this field has no actual users, and it is generally
just to display users in a more friendly way on the page. The
nickname
field is allowed to be repeated in the data table.
# score
This represents the user's points, and the type is an integer.
# avatar
This represents the user's avatar, the value is the tid
of the
material corresponding to the avatar, and the default is 0
(that is,
no avatar is set).
# password
This field stores the hashed user password.
# ip
This field records the IP address of the user when registering. This IP address is used to prevent multiple registrations with the same IP address. Most plugins do not use this field.
# permission
This field records the permission level of the user, and its type is an integer. Currently four values are possible:
- - the value is
-1
, you can also use the enumeration valueUser::BANNED
to indicate - Normal - the value is
1
, you can also use the enumeration valueUser::NORMAL
to represent - Admin - The value is
1
, you can also use the enumeration valueUser::ADMIN
to represent - Super administrator - the value is
2
, you can also use the enumeration valueUser::SUPER_ADMIN
to represent
# last_sign_at
The time of the last check-in is recorded.
# register_at
Records the time the user registered. Most plugins do not use this field.
# verified
This field records whether the email address has been verified. Most plugins do not use this field.
# verification_token
The value of this field is used for proofreading when validating email addresses. This field has been deprecated since Blessing Skin v4. Most plugins do not use this field.
# remember_token
This field is used to identify whether the "remember me" option is enabled when the user logs in. This field is only available since Blessing Skin v4. Most plugins do not use this field.
User
model instance are described below .
# isAdmin
Get the information "whether the user is an administrator".
Parameters: none
Return value: the type is bool
, indicating whether the user is an
administrator. (Super administrators are also administrators)
# verifyPassword
Check that the password is correct.
parameter:
rawPassword
- the raw password used for comparison (that is, the password that has not been hashed)
Return value: the type is bool
, indicating whether the password is
correct.
# changePassword
Change a user's password. Usually plugins don't use this method.
parameter:
password
- the new unhashed password
Return value: The type is bool
, indicating whether the password is
changed successfully.
# Player
model
Player
model represents a role, and each model instance records
information about a role. The primary key of the Player
model in the
database is pid
, which is a self-increasing integer field, so you
cannot modify a user's pid
because it is a unique identifier.
The following will introduce the meaning of each data attribute on the
Player model.
# pid
This represents the unique identifier of each role, and you cannot
modify the value of this property. At the same time, because it is a
unique identifier, a pid
corresponds to a role.
# uid
uid
of the role owner .
# name
This field records the role name.
# tid_skin
tid
of the material corresponding to the skin on the character .
# tid_cape
This field records the
tid of the material corresponding to the cloak on the character
.
# last_modified
This field records the time when the role last modified information. The value of this field is more used by skin mods, and this field is rarely used by plugins.
The following introduces some methods on the Player
model instance.
# Texture
model
Texture
model represents the material, and the data of each material
is recorded on the model instance. The primary key of the
Texture
model in the data table is tid
, which is a self-increasing
integer field, so you cannot modify the tid
of a material because it
is a unique identifier.
The following will introduce the meaning of each data attribute on the
Texture model.
# tid
This represents the unique identifier of each material, and you cannot
modify the value of this property. At the same time, because it is a
unique identifier, a tid
corresponds to a material.
# name
This field records the name of the material.
# type
This field records the type of material. It can only be steve
or
alex
or cape
# hash
This field records the hash value of the material file, which is unique (unless there is a collision in the hash algorithm).
# size
This field records the size of the material file, and the unit is KB
.
# uploader
uid
of the material uploader . It allows pointing to non-existing
users, but the field cannot be empty in the database.
# public
This field indicates whether the material is a public material. Note
that the records of this field in the database may be stored in the form
of numbers (that is, 0
or 1
), but when this attribute is actually
retrieved in the code, it will be automatically converted into a
bool
type.
# upload_at
This field records the upload time of the material.
The following introduces some methods on the Texture
model instance.
# setPrivacy
Modifies the visibility of the material.
parameter:
public
- typebool
, indicating whether this material can be made public.
Return value: the type is bool
, indicating whether the change is
successful.
$ texture->setPrivacy( true ) ; // Texture has been set to public
$texture ->setPrivacy( false ) ; // material has been set as private
# Model-to-Model Links
# associate
The relationship between users and roles is "one-to-many", that is, a user can have multiple roles. If we have already got a model instance of a user, and now we want to get all the role instances of the user, we can write like this:
$players = $user- >players ;
Conversely, we can look up its owner by a role, like this:
$owner = $player- >user ;
That's it, you don't have to manually look it up by uid
or pid
:
// There's no need to do something like this!
$uid = $user- >uid ;
$players = P layer::where( 'uid' , $uid )->get() ;
# Notice
Reading properties as shown above will get all the role instances of the
user, if you want to filter by conditions or do other operations, then
you should call their methods instead of properties. For example, if you
want to find roles whose model is slim
among all roles of a user, you
should write:
$players = $user- >players()->where( 'preference' , 'slim' ) ;
more information on model associations, visit the Laravel documentation (opens new window) .