本文介绍了在Laravel迁移中截断表时,应该在down函数中添加什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个迁移脚本来截断我的 visitors 表.通常,我们将up函数的还原放置在down函数中.

I'm trying to write a migration script to truncate my vistors table.We usually place the revert of the up function in the down function.

但是在这种情况下,它是截断,我应该在down函数中添加什么?

But in this case, it's a truncate, what should I put in my down function ?

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class TruncateVisitorsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
       DB::table('visitors')->truncate();
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // what should I put here ? 
    }
}

推荐答案

以下是我使用的一个示例:

Here is one example I use:

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        //
    });

    Schema::create('cats', function(Blueprint $table)
    {
        //
    });

    Schema::create('items', function(Blueprint $table)
    {
        //
    });
}

public function down()
{
    $tables = [
        'users',
        'cats',
        'items'
    ];

    foreach($tables as $table) Schema::drop($table);
}

您将在播种器的开头使用 truncate .

You would use truncate in the beginning of a seeder.

这篇关于在Laravel迁移中截断表时,应该在down函数中添加什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 01:40