tweet
モデルとimage
モデルから、4件の画像image
モデルをもつ、ダミーデータをtweet
モデルを通じて出力しています。
ImageFactory
でfaker
を通じて、imageメソッドによる該当のディレクトリに画像を出力されるように構成してから、TweetsSeeder
から4件の画像を紐づけるようにシーディングを行いました。
しかし、images
テーブルのname
カラムにおいてfaker
によるデータを保存し、そこにfakerによる画像が反映され、・・・b3.png
などとなるはずですが、どの値も0
になってしまします。
ちなみにstorage/app/public/images
に出力されるはずでそこには何にも反映されませんでした。
教科書通りにソースコードも全てコピーで再度反映させたはずですが、何がいけないのでしょうか?ご教授いたただけたらお願いいたします。
バージョンはrequire a PHP version ">= 8.1.0". You are running 8.0.19
でした。
sql
mysql> select * from images; +----+------+---------------------+---------------------+ | id | name | created_at | updated_at | +----+------+---------------------+---------------------+ | 1 | 0 | 2022-09-08 05:21:15 | 2022-09-08 05:21:15 | | 2 | 0 | 2022-09-08 05:21:15 | 2022-09-08 05:21:15 | | 3 | 0 | 2022-09-08 05:21:15 | 2022-09-08 05:21:15 | | 4 | 0 | 2022-09-08 05:21:15 | 2022-09-08 05:21:15 | | 5 | 0 | 2022-09-08 05:21:16 | 2022-09-08 05:21:16 | | 6 | 0 | 2022-09-08 05:21:16 | 2022-09-08 05:21:16 | | 7 | 0 | 2022-09-08 05:21:16 | 2022-09-08 05:21:16 | | 8 | 0 | 2022-09-08 05:21:16 | 2022-09-08 05:21:16 | | 9 | 0 | 2022-09-08 05:21:17 | 2022-09-08 05:21:17 | | 10 | 0 | 2022-09-08 05:21:17 | 2022-09-08 05:21:17 |
imageFactory.php
php
1<?php 2 3namespace Database\Factories; 4 5use Illuminate\Database\Eloquent\Factories\Factory; 6use Illuminate\Support\Facades\Storage; 7 8/** 9 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Image> 10 */ 11class ImageFactory extends Factory 12{ 13 /** 14 * Define the model's default state. 15 * 16 * @return array<string, mixed> 17 */ 18 public function definition() 19 { 20 if (!Storage::exists('public/images')) { 21 Storage::makeDirectory('public/images'); 22 } 23 return [ 24 'name' => $this->faker->image(storage_path('app/public/images'), 640, 480, null, false) 25 ]; 26 } 27} 28
TweetsSeeder.php
php
1<?php 2 3namespace Database\Seeders; 4 5use Illuminate\Database\Console\Seeds\WithoutModelEvents; 6use Illuminate\Database\Seeder; 7use Illuminate\Support\Facades\DB; 8use Illuminate\Support\Str; 9use App\Models\Tweet; 10use App\Models\Image; 11 12class TweetsSeeder extends Seeder 13{ 14 public function run() 15 { 16 Tweet::factory()->count(10)->create()->each( 17 fn ($tweet) => 18 Image::factory()->count(4)->create()->each( 19 fn ($image) => 20 $tweet->images()->attach($image->id) 21 ) 22 ); 23 } 24} 25
models/Tweet.php
php
1<?php 2 3namespace App\Models; 4 5use Illuminate\Database\Eloquent\Factories\HasFactory; 6use Illuminate\Database\Eloquent\Model; 7 8class Tweet extends Model 9{ 10 use HasFactory; 11 12 public function user() 13 { 14 return $this->belongsTo(User::class); 15 } 16 public function images() 17 { 18 return $this->belongsToMany(Image::class, 'tweet_images')->using(TweetImage::class); 19 } 20} 21
追記コード
database/migrations/***_create_images_table.php
php
1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7return new class extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('images', function (Blueprint $table) { 17 $table->id(); 18 $table->string('name'); 19 $table->timestamps(); 20 }); 21 } 22 23 /** 24 * Reverse the migrations. 25 * 26 * @return void 27 */ 28 public function down() 29 { 30 Schema::dropIfExists('images'); 31 } 32}; 33
database/migrations/***_create_tweet_images_table.php
php
1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7return new class extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('tweet_images', function (Blueprint $table) { 17 $table->foreignId('tweet_id')->constrained('tweets')->cascadeOnDelete(); 18 $table->foreignId('image_id')->constrained('images')->cascadeOnDelete(); 19 $table->timestamps(); 20 }); 21 } 22 23 /** 24 * Reverse the migrations. 25 * 26 * @return void 27 */ 28 public function down() 29 { 30 Schema::dropIfExists('tweet_images'); 31 } 32}; 33