Câu hỏi phỏng vấn Laravel
Câu hỏi

How do you generate migrations?

Câu trả lời

To generate migrations in Laravel, you can use the built-in Artisan command make:migration. This command helps you create a new migration file, which will be placed in the database/migrations directory of your Laravel project. Each migration file contains a timestamp in its filename to determine the order of migrations. Here is a step-by-step guide on how to generate migrations:

Basic Migration Creation

  1. Create a Migration File:
    To create a new migration file, you can use the following command:

    bash Copy
    php artisan make:migration create_users_table

    This command will generate a migration file with a timestamp prefix in the database/migrations directory. Laravel will attempt to determine the table name and the migration action from the filename.

  2. Specify Table and Action:
    If Laravel cannot determine the table name or action, you can specify them using the --table and --create options:

    bash Copy
    php artisan make:migration add_votes_to_users_table --table=users
    php artisan make:migration create_users_table --create=users
  3. Custom Path:
    You can also specify a custom path for the generated migration file using the --path option:

    bash Copy
    php artisan make:migration create_users_table --path=app/database/migrations

Migration Structure

A migration class contains two main methods: up and down.

  • up Method:
    This method is used to define the operations to be performed when the migration is run. For example, creating a new table:

    php Copy
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamps();
        });
    }
  • down Method:
    This method is used to reverse the operations performed by the up method. For example, dropping the table:

    php Copy
    public function down()
    {
        Schema::dropIfExists('users');
    }

Running Migrations

...

middle

middle

Gợi ý câu hỏi phỏng vấn

junior

List some official packages of Laravel

senior

Where can you inject authentication checks on an API request?

junior

Which template engine does Laravel use?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào