2
1

More than 3 years have passed since last update.

Laravelの中間テーブルでcreated_atとupdated_atがnullの場合の対処

Posted at

中間テーブルにデータを挿入する際に、created_atやupdated_atが挿入されず、Field 'created_at' doesn't have a default valueField 'updated_at' doesn't have a default valueが表示される場合の対処です。
スクリーンショット 2020-12-05 22.21.25.png

動作環境

こちらのDocker環境を使用しています。
最強のLaravel開発環境をDockerを使って構築する【新編集版】

マイグレーションファイル

2020_12_05_000000_create_feed_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateFeedTable extends Migration
{
    public function up()
    {
        Schema::create('feed', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamp('updated_at');
            $table->timestamp('created_at');
            $table->string('name');
        });
    }

    public function down()
    {
        Schema::dropIfExists('feed');
    }
}
2020_12_05_000001_create_penguin_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePenguinTable extends Migration
{
    public function up()
    {
        Schema::create('penguin', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamp('updated_at');
            $table->timestamp('created_at');
            $table->string('name');
        });
    }

    public function down()
    {
        Schema::dropIfExists('penguin');
    }
}

シーディングファイル

FeedTableSeeder.php
<?php
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use DateTime;

class FeedTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('feed')->insert([
            'created_at' => new DateTime(),
            'updated_at' => new DateTime(),
            'name' => 'イワシ',
        ]);
    }
}
PenguinTableSeeder.php
<?php
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use DateTime;

class PenguinTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('penguin')->insert([
            'created_at' => new DateTime(),
            'updated_at' => new DateTime(),
            'name' => 'イワトビペンギン',
        ]);
    }
}

中間テーブル

上記の餌テーブルとペンギンテーブルの中間テーブルのfeed_penguinのマイグレーション。

2020_12_05_000003_create_feed_penguin_table.php
<?php

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

class CreateFeedPenguinTable extends Migration
{
    public function up()
    {
        Schema::create('feed_penguin', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamp('updated_at');
            $table->timestamp('created_at');
            $table->unsignedInteger('feed_id')->unsigned();
            $table->unsignedInteger('penguin_id')->unsigned();
            $table->foreign('feed_id')->references('id')->on('feed');
            $table->foreign('penguin_id')->references('id')->on('penguin');
        });
    }

    public function down()
    {
        Schema::dropIfExists('feed_penguin');
    }
}

モデルファイル

Feed.php
<?php
namespace App\Models\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use app\Models\Models\Penguin;

class Feed extends Model
{
    use HasFactory;

    protected $table = 'feed';

    public function penguins() {
        return $this->belongsToMany('Penguin');
    }
}
Penguin.php
<?php
namespace App\Models\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use app\Models\Models\Feed;

class Penguin extends Model
{
    use HasFactory;

    protected $table = 'penguin';

    public function feeds() {
        return $this->belongsToMany('Feed');
    }
}

中間テーブルにリレーションを紐付け

tinkerを使って、リレーションを紐付けしてみます。

php artisan tinker

$penguin = App\Models\Models\Penguin::first()
$penguin->feeds()->attach(1)

このままでは、created_atやupdated_atがnullとなるので、エラーが出てinsertされないので、中間テーブルのマイグレーションのcreated_atとupdated_atの項目を修正します。

中間テーブルのマイグレーション修正

2020_12_05_000003_create_feed_penguin_table.php
$table->timestamp('updated_at')->useCurrent();
$table->timestamp('created_at')->useCurrent();

これで、中間テーブルにcreated_atupdated_atが挿入されます。
スクリーンショット 2020-12-05 22.26.19.png
スクリーンショット 2020-12-05 22.27.57.png

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1