--directory
flag to specify the location of these modelsusers
table is assumed and if you have a model like Company
then the companies
table is assumed. You can realize that Masonite ORM is smart enough to know that the plural of Company
is not Companys
so don't worry about Masonite not being able to pick up your table name.__table__
attribute:id
. You can change the primary key name easily:default
connection you setup in your configuration settings. You can also change this on the model:created_at
and updated_at
columns on your table. You can easily disable this behavior:UTC
as the default timezone. You can change the timezones on your models using the __timezone__
attribute:find
method:pluck
be sure to read the Collections documentation.SELECT *
queries. You can change this behavior in a few ways.__selects__
attribute with a list of column names. You may use the as
keyword to alias your columns directly from this list:all()
method:get
method as well:{method_name}_id -> id
. You can change the relating columns if that is not the case:Note the keys here are flipped. This is the only relationship that has the keys reversed
product_store
that is in between stores and products.belongs_to_many
relationship to get all the products of a store easily. Let's start with the Store
model:stores
directly to products
:product_store
) where the store id and the product ID match. By default this attribute is pivot
.id
. This is used to hydrate the record so you can update the pivot records. If you do not have a pivot primary key you can turn this feature off:id
:persons
table and a houses
table then the table name is assumed to be house_person
. You can change this naming:updated_at
and created_at
) on your pivot table. If you would like Masonite to manage timestamps you can:pivot
. You can change this:created_at
and updated_at
.HasOneThrough
relationship defines a relationship between 2 tables through an intermediate table. For example, you might have a Shipment
that departs from a port and that Port
is located in a specific Country
.Shipment
could be related to a specific Country
through a Port
.Country
as the first list element. The second element is the intermediate table that we need to get from Shipment
to Country
. In this case that is the Port
model so we put that as the second element in the list.HasManyThrough
relationship defines a relationship between 2 tables through an intermediate table. For example, you might have a "user" that "likes" many "comments".User
could be related to multiple Comment
through a Like
.Comment
as the first list element. The second element is the intermediate table that we need to get from User
to Comment
. In this case that is the Like
model so we put that as the second element in the list.with_count
method can be used to get the number of records in a relationship.{relationship}_count
attribute. You can get this attribute like this:Like
could be associated to a Comment
or an Article
.DB
variable in your database config file:images
table has record_type
and record_id
fields. These could be named anything but it should contain a varchar type column that will be used to map to a model as well as a column to put the foreign tables primary key value.likes
table has record_type
and record_id
fields. These could be named anything but it should contain a varchar type column that will be used to map to a model as well as a column to put the foreign tables primary key value.likes
table still has one
relationship to multiple models but the relating tables ("articles" and "comments" has many
records to the likes
table).morph_to
and a morph_to_many
relationships. This is used to relate multiple records to the polymorphic table. These relationships are used on the polymorphic model to relate to the related models. The morph_to
will return 1 result from the related model and the morph_to_many
would return multiple.__with__
attribute on the model:.
notation:joins
method:join
method.join_on
method:SoftDeletesMixin
scope class:deleted_at
record from the table to the current timestamp:deleted_at
datetime field to your database table for this feature to work.soft_deletes()
helper that you can use in migrations to add this field quickly.deleted_at
you can change the column to a different name:force=True
to update()
method__force_update__
attribute on the model classforce_update()
method on model:Joe
and active of 1
.updated_at
column will be automatically updated. You can control this behaviour by using activate_timestamps
method:Note that this will only create a new model instance but will not contain any additional fields on the table. It will only have whichever fields you pass to it.
find
method to get the full record. Let's say you have a scenario in which the active
flag defaults to 1 from the database level. If we create the record, the active
attribute will not fetched since Masonite ORM doesn't know about this attribute..fresh()
after create:__appends__
will be added__hidden__
attribute on your model:__visible__
attribute on your model to explicitly tell which fields should be included in serialization:__hidden__
and __visible__
on the model.UUIDPrimaryKeyMixin
scope: