Migration anatomy

Consider an example of one of the lines in the migration file; we can see that the table object is used in a chain of methods. The following line of the migration file sets up the state attributes in the location eloquent attribute in the locations table:

$table->smallInteger('state_id')->unsigned()->index('state_id'),

List tables

Often, it is necessary to create or import a list of finite items that usually remain constant, such as cities, states, countries, and similar items. Let's call these list tables or lookup tables. In these tables, the ID should usually be positive. These lists may grow, but they usually will not have any data that is deleted or updated. The smallInteger type is used to keep the table small and also represent a value that belongs to a finite list, something that will not grow naturally. The next method, unsigned, states that the limit will be 65535. This value should be enough to represent most of the states, provinces, or similar types of geographical regions where a hotel could be located. The last method in the chain adds an index to the database column. This is essential in list tables like these, which are used in the select statements or in the read statements. The Read statements will be discussed in Chapter 9, Scaling Laravel. It is important to use unsigned, as it doubles the positive limit, which otherwise would be 32767. Using the index, we can speed up the look up time and access a cached version of the data in the table.

The softDelete and timestamp properties

Regarding softDeletes and timestamps for list tables, it depends. If the table is not very large, it shouldn't be too harmful to keep track of any updates, inserts, or deletions if they do happen; however, if the list consists of countries, where changes occur infrequently and are very small, it would be prudent to omit softDeletes and timestamps. So, the entire table will probably fit into the memory and will be very fast. To omit timestamps, it's necessary to add the following line of code:

public $timestamps = false;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset