中間テーブルにデータを挿入する際に、created_atやupdated_atが挿入されず、Field 'created_at' doesn't have a default value
やField 'updated_at' doesn't have a default value
が表示される場合の対処です。
動作環境
こちらの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();