markdown 在Laravel 5.7种子文件中截断具有外键约束的表。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 在Laravel 5.7种子文件中截断具有外键约束的表。相关的知识,希望对你有一定的参考价值。

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class DatabaseSeeder extends Seeder
{
    // Your tables to truncate
    protected $toTruncate = ['user'];

    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // This is a simple strategy for truncate your database.
        
        // Disabled massive assignament restriction
        Model::unguard();
        // Disable foreign key check for this connection before running seeders
        Schema::disableForeignKeyConstraints();

        // Note: truncate strategy failed with SQL Server (and others).
        foreach($this->toTruncate as $table) {
            DB::table($table)->delete();
        }
        
        /**
        * Your factories exections.
        * For example:
        * ************
        *
        * factory(User::class, 1000)->create();
        *
        */

        // Enable constraints
        Schema::enableForeignKeyConstraints();
        // Enable massive assignament restriction
        Model::reguard();
    }
}
Please, don't use for disable foreign key constraints:

```php
  DB::statement('SET FOREIGN_KEY_CHECKS=0;');
  // and
  DB::statement('SET FOREIGN_KEY_CHECKS=1;');
```
This is a MySQL specific statement, and your are using a ORM. The previous statement doesn't work in other database system (for example: MSSQL). Use something like what I describe in my *DatabaseSeeder.php* file.

以上是关于markdown 在Laravel 5.7种子文件中截断具有外键约束的表。的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 4 db 种子特定播种器文件

如何使用 laravel 5.7 和 vue JS 在 mysql 中导入 excel 文件

laravel 5.7 中的多个文件上传问题

如何修复“文件上传失败。”使用任何图像上传验证时出错 - Laravel 5.7

为啥不能在 Laravel 5.7 中捕获我定义的 Route 请求的文件 URI?

为啥我得到一个 404 到 laravel 5.7 公共子文件夹?