Go to Home

Laravel Migration : Things to do

Laravel Migration is one of the best feature that helps developers to deploy their database tables into production server without any hassle. For people like me, who started their career initially with code igniter framework might find this feature as a blessing. I remember the pain I went through for creating tables for the product which we had been developing in code igniter. As soon as I came to know about laraval migration, I was completely blown away by seeing how easy it is to create and manage tables.

In this article, I’m not going to explain you how to do laravel migration(which you can learn through laravel documentation), instead we are going to focus on things like optimizing database tables, improving readability etc.

Naming

During table creation, laravel advises to create table name as plurals and model name as singular. For example, when you keep your model as User, then your table name should be users, when your model name is Address, then your table name should be addresses. That is naming convention that laravel prefers. In case, you can’t choose how to name your tables, then you can leave it to laravel. I mean, just create the model and laravel will create your tables accordingly.

php artisan make:model User -m

Above command will create you model file with name User and will also generate a migration file that has table name as users.

Indexing

Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. The larger the table, the more this costs. If the table has an index for the columns in question, MySQL can quickly determine the position to seek to in the middle of the data file without having to look at all the data. This is much faster than reading every row sequentially.

It is always advised to index the columns that are often used during quering. By default, primary keys, unique keys are indexed.

To know more about indexing in laravel, check out its documentation here.

Data Types

I have seen people using wrong datatypes, while defining table structure. For example, when you need a column to represent flag, giving datatypes as integer or string is wrong, TINYINT is the right one. Same way, for address giving TEXT datatype is wrong, we can use VARCHAR. TEXT is used when we need to save a larger content. So, giving right datatype might increase performance. Check the below links for better understanding.

For databases, does choosing the correct data type affect performance?

Designing Databases: Picking The Right Data Types

Foreign Key Reference

During table creation, whenever we add a foreign key column that represents the primary key of some other table, it is good practice to add delete on cascade functionality. Thus during deletion, instead of handling delete foreign key mapping manually, we make mysql to do the task for us.

That’s it! Above are the list of things I wanted to share about laravel migration.

Happy Coding!