3-minute essay: I've never met before. Introduction to Laravel database model


The framework without model is not flexible! Each framework will carry out in-depth design for the database,
In order to quickly add, delete, modify and check SQL, abstract the repeated work and improve the efficiency of application development.

In this issue, we will talk about the initial use of laravel's model.

Code time

The model is a layer of abstraction in the code to extract the database operation. laravel supports common database products,
Let's take MySQL as an example. First, in the configuration file, configure the parameters to connect to the database.

In the configuration file config / database In PHP, the default configuration is as follows:

Including database connection name, database address, port, user name and password, database name and other information, the initialization configuration is completed.
Generally, if it is not specified manually, the $connection in the laravel model is the line in the configuration file:

'default' => env('DB_CONNECTION', 'mysql'),

That is, mysql database. Then we create a migration file through the migration function. This eliminates the need for us to manually create database tables.
Run the following command on the command line:

php artisan make:model Event --migration

The command line output is as follows:

Model created successfully.
Created Migration: 2020_09_27_202416_create_events_table

You can see that the system has created a model file in APP / event php. The content varies according to different versions of laravel, but the basic content remains unchanged:

With the database table corresponding to the model, we still lack a database table, which can be configured in the migration file.
Because we used the -- migration option in the above command line scaffold, the system created the migration file,
Edit 2020_09_27_202416_create_events_table.php content. The contents included are as follows:

The up method Schema is used to create database fields, indexes, etc., and the down method is used to rollback migration.
In life, two fields are given by default:

  • Self increasing id field
  • The timestamps() method creates created_at and updated_at two datetime fields

Let's add some more fields to the database table:

The string method creates a field of VARCHAR type, and the TEXT method creates a field of TEXT type.
nullable method table name this field is allowed to be NULL by default.

After completing the field design of the form, we apply the designed table to the database. Execute the following commands on the command line:

php artisan migrate

The command line output is roughly as follows:

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrating: 2020_09_27_202416_create_events_table
Migrated: 2020_09_27_202416_create_events_table

users table, password_ The resets table is used by the system with its own permissions.

To verify whether the migration is successful, we use mysql client to connect to the target database on the command line.
First show all table names:

Let's focus on whether the structure of the events table we designed is as expected by Schema:

If we need to redesign the table structure, it is also easy to roll back the above migration. Execute the following command:

php artisan migrate:rollback

The output contents are as follows:

Rolling back: 2020_09_27_202416_create_events_table
Rolled back: 2020_09_27_202416_create_events_table
Rolling back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolling back: 2014_10_12_000000_create_users_table
Rolled back: 2014_10_12_000000_create_users_table

This is not good. We just want to roll back the events table, not the user password_resets table.
Then we need to manually specify that when rolling back, only the latest batch of migration will be rolled back. Execute the following code:

php artisan migrate:rollback --step=1

The output is as follows:

Rolling back: 2020_09_27_202416_create_events_table
Rolled back: 2020_09_27_202416_create_events_table

Note that it is rolled back according to the batch. If you roll back to the second batch and the third batch, only specify -- step=N.

Write at the end

This article explains the contents of laravel in connecting to the database, creating database tables and migrating files. For the powerful Eloquent ORM, this is just the beginning.

Happy coding 😃

I am @Programmer assistant , an original author in IT field who focuses on programming knowledge and has a dynamic circle

Tags: PHP MySQL Laravel

Posted by verano on Sat, 14 May 2022 06:02:55 +0300