Code Guide 2018/10/16
Models
Models extend the Illuminate\Database\Eloquent\Model class, reflecting the data structure –
the way they are stored in the database.
Also, models using Trait
Backpack\CRUD\CrudTrait are manageable from the administration panel.
Models
using Trait App\Traits\Translatable have some translatable fields, and are stored in
another Model with the name ModelNameTranslation (for example: Product &
ProductTranslation).
Properties
- $fillable: fields that can be batch written in the database. It is required to specify all the fields which must be editable from the administration panel.
- $translatedAttributes: translatable fields that are stored in the Translation.
- $appends: fields not stored in the database, that are calculated at the time of loading the model and must be appended to all representations of this model.
- $visible: in some models that can be transferred via Ajax, we do not want to append all the fields to that model out of security or privacy concerns. In such a case, we specify the fields we do need, and the rest will remain hidden.
- $timestamps: true by default, indicates that this table has the created_at and updated_at columns, which will be updated automatically whenever the model is created or modified. When false, it indicates that these fields do not exist in this table and, therefore, Eloquent must not try to update them.
- $rememberTokenName: name of the field which stores the token to remember the user in models using the Authenticatable trait. Since this application does not use this feature, in the user models this property is ‘’ (empty string).
- $table: by default, the table corresponding to a model has the same name of the model converted to snake_case. If this is not the case for some model, it is necessary to specify it here.
- $dates: this indicates which table fields use the date format. By default, it is created_at and updated_at. If there are more fields of this date type, they must be specified here.
- $casts: Laravel automatically converts these fields to the type specified here, which is useful for json fields.
- $fakeColumns: the CRUD package uses this property to save virtual fields as json values in a database field.
Relationships
The relationship functions specify how this model relates to other models in the database. They can be of the following types:
- hasMany: one-to-many.
- belongsTo: inverse of hasMany.
- belongsToMany: many-to-many. This type of relationship means that there is an intermediate pivot table which, by default, is named model1_model2 (in alphabetical order). If it is not this way, the table name must be specified.
- morphOne: polymorphic relationship one-to-one. Used for storing SEO data.
- morphTo: inverse of morphOne.
Accessors
Accessors allow us to add calculated fields to a model, and access them as if they were part
of the corresponding database record. If they have the same name as an existing field on a
table, they allow for modification of the presentation of said field before showing it to
the user. They follow the nomenclature:
public function
getFieldNameAttribute()
and are accessed with:
$model->field_name;
Mutators
Mutators allow field modification before saving it to the database. The nomenclature is:
public
function setFieldNameAttribute($value)
and are accessed with:
$model->field_name
= ‘value’;
Existing models in the application and correspondence to administration panel controllers
- App (tables: apps, app_translations; admin: AppCrudController):
Mobile apps shown on “Apps” page. - BusinessRange (table: business_ranges, business_range_translations; admin:
BusinessRangeCrudController):
Ranges shown on “In your business” page. - BusinessRangeElement (table: business_range_elements
business_range_element_translations; admin: BusinessRangeCrudController child
field):
Business range page elements. They are managed from the Business Range admin panel. - Contact (table: contacts; admin: ContactCrudController):
Contact requests sent by front-end users. - ContactMailto (table: contact_mailtos, contact_mailto_translations; admin:
ContactMailtoCrudController):
Mailto links list on Contact page. - ContactType (table: contact_types, contact_type_translations; admin:
ContactTypeCrudController):
Contact types for Contact page form dropdown. - Country (table: countries; admin: CountryCrudController):
Countries for Contact page form dropdown - DataTable (table: data_tables, data_table_translations; admin:
ProductCrudController child field):
Technical data for products. Managed from the Product admin panel. - DataTableElement (table: data_table_elements, data_table_element_translations;
admin: ProductCrudController child field):
Technical data for products loaded from a csv or xls file. Managed from the Product/Datatable admin panel. - DataTableCategory (table: data_table_categories,
data_table_category_translations; admin: ProductCrudController child field):
Categories technical data is organized in data tables of type “multiple”. - Distributor (table: distributors; admin: DistributorCrudController):
Contact information of installers, distributors and technical service places, shown on the Maps pages. - DistributorType (table: distributor_types):
Types of suppliers, to select which type of supplier will be shown on the map. Fixed, non-manageable values: distributor, installer, technical_service. - DistributorSubtype (table: distributor_subtypes):
Subtypes of suppliers, to select which subtype of supplier will be shown on the map when the main type is installers. Fixed, non-manageable values: ac-installer, pump-installer. SEO and translations are available. - Download (table: downloads, download_translations; admin: DownloadCrudController):
Resources available to download. - DownloadCategory (table: download_categories, download_category_translations;
admin: DownloadCategoryCrudController):
Categories downloads are organized in. - DownloadSubCategory (table: download_subcategories,
download_subcategory_translations; admin: DownloadCategoryCrudController child
field):
Subcategories in which downloads are organized. Managed from the Download Category admin panel. - DownloadType (table: download_types, download_type_translations; admin:
DownloadTypeCrudController):
Types of downloads in which downloads are organized. - DownloadFrontendUser (table: download_frontend_user; admin:
DownloadFrontendUserCrudController):
Register of which users downloaded which resources - FrontendUser (table: frontend_users; admin: FrontendUserCrudController):
Users can download resources when they register and sign in with their email and password. - HomeRange (table: home_ranges, home _range_translations; admin:
HomeRangeCrudController):
Ranges shown on “In your home” page. - HomeRangeElement (table: home _range_elements, home _range_element_translations;
admin: HomeRangeCrudController child field):
Home range page elements. They are managed from the Business Range admin panel. - Image (table: images; admin: ImageCrudController):
Modifiable front-end images. - Landing (table: landings; admin: LandingCrudController):
Distribution page configuration. Distribution page publishing authorizations are managed with LandingRequestAuthController - LandingLink (table: landing_links; admin: LandingCrudController child field):
Distribution page links. They are managed from the Distribution page admin panel. - LandingContact (table: landing_contacts; admin: LandingContacCrudController child
field):
Contact requests sent by distribution page front-end users. - Language (table: languages; admin: LanguageCrudController):
Available languages for translations. - Legal (tables: legals, legal_translations; admin: LegalCrudController):
Legal links shown in the footer. - News (table: news, news_translations; admin: NewsCrudController):
Magazine news. - NewsCategory (table: news_categories, news_category_translations; admin:
NewsCategoryCrudController):
Categories in which news is organized. - Office (table: offices; admin: OfficeCrudController):
Offices listed in the “About” section. - Page (table: pages, pages_translations; admin: PageCrudController):
Every page shown in the front-end.
This application works as a CMS. Each page has a config json field that stores:
- From which Model it loads its main data (optional).
- What view file it uses.
- Information about secondary elements to load (optional).
- What page is its “child” page (optional).
- Optional access restrictions (for example only logged in users can download resources, and only NOT logged in users can login or register).
- Information about other data used in this page, saved in the data and t_data fields.
The PageTranslation Model has a t_data field that stores translatable texts used in this page.
- PageElement (table: page_elements, page_element_translations: admin
PageCrudController child field):
Child elements managed from the page admin panel. Only the Home Page has elements. The element types are fixed: slider, in-your-home and in-your-business. - PageElementImage (table: page_element_images, page_element_image_translations:
admin PageCrudController child field):
Page elements related images. - Product (table: products, product_translations; admin: ProductCrudController):
Products. They can be linked to several home and business ranges. They can have related downloads. - ProductElement (table: product_elements, product_element_translations; admin:
ProductCrudController child field):
Product elements. They are managed from the Products admin panel. - ProductElementImage (table: product_element_images,
product_element_image_translations; admin: ProductCrudController child field):
Product elements related images. They are managed from the Product/Elements admin panel. - ProfileType (table: profile_types, profile_type_translations; admin:
ProfileTypeCrudController):
Profile types for Register Professional page form dropdown. - Project (table: projects, project_translations; admin: ProjectCrudController):
Projects listed on Project page. - ProjectCategory (table: project_categories, project_category_translations; admin:
ProjectCategoryCrudController):
Categories in which projects are organized. - Range (table: ranges, range_translations; admin: RangeCrudController):
Ranges shown on “Equipment” page. - RangeElement (table: range_elements, range_element_translations; admin:
RangeCrudController child field):
Range page elements. They are managed from the Range admin panel. - Seo (table: seos, seo_translations):
SEO data is managed from each Seoable Model Seo Tab. - Setting (table: settings; admin: SettingCrudController):
Site settings. - Social (tables: socials, social_translations; admin: SocialCrudController):
Social networks links. - State (tables: states, state_translations; admin: StateCrudController):
States/provinces for Register page form dropdown. - Tag (table: tags; admin: TagCrudController):
Tags used to relate news articles and downloads. - Text (table: texts, text_translations; admin: TextCrudController):
Modifiable front-end texts. - User (table: users; admin: UserCrudController):
Admin users with access to the administration panel with their email and password.