質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

2回答

866閲覧

Laravelでfakerのimageメソッドで画像をシードすると値が0になる

JuniorSirius

総合スコア38

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

0クリップ

投稿2022/09/07 21:00

編集2022/09/08 20:32

tweetモデルとimageモデルから、4件の画像imageモデルをもつ、ダミーデータをtweetモデルを通じて出力しています。

ImageFactoryfakerを通じて、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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2022/09/08 00:52

テーブル定義はどうなっていますか? また、画像情報は想定どおり取得できていますか?デバッグしてみてください
JuniorSirius

2022/09/08 20:42 編集

migrationファイルを追記いたしました。migrationを再度適応させたり、シードの入れ直しはしましたが相変わらずimagesテーブルのfakerの出力データが0になり、画像も反映されません。 テーブルはtweets、images、tweet_imagesで交差テーブルを設けています。 mysql> show columns from tweets; +------------+-----------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+-----------------+------+-----+---------+----------------+ | id | bigint unsigned | NO | PRI | NULL | auto_increment | | user_id | bigint unsigned | NO | MUL | NULL | | | content | varchar(255) | NO | | NULL | | | created_at | timestamp | YES | | NULL | | | updated_at | timestamp | YES | | NULL | | +------------+-----------------+------+-----+---------+----------------+ mysql> show columns from tweet_images; +------------+-----------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+-----------------+------+-----+---------+-------+ | tweet_id | bigint unsigned | NO | MUL | NULL | | | image_id | bigint unsigned | NO | MUL | NULL | | | created_at | timestamp | YES | | NULL | | | updated_at | timestamp | YES | | NULL | | +------------+-----------------+------+-----+---------+-------+ mysql> show columns from images; +------------+-----------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+-----------------+------+-----+---------+----------------+ | id | bigint unsigned | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | NULL | | | created_at | timestamp | YES | | NULL | | | updated_at | timestamp | YES | | NULL | | +------------+-----------------+------+-----+---------+----------------+ でした。 1番の関心はシーディングした際に、質問コードのTweetsSeederシーダークラスの画像がFactoryから反映されずに設けてあるnameカラムにデータとストレージに反映されないという点です。 fakerが動作しないように見えます。
guest

回答2

0

対応方法は私も調べ中ですが、自分で用意した画像を参照するようにする感じですかね?
対応方法は私も調べ中ですが、faker使わないのが無難そうですね。

投稿2023/01/15 14:46

matsu_ura

総合スコア5

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

さっき、私も同じ現象でつまずいていたのですが、調べてみると以下の記事を見つけました。
https://su-kun1899.hatenablog.com/entry/2022/09/10/220000

-----上記の記事より抜粋-----

FakerPHP/Faker は v1.19.0 を利用していたのだが、v1.20.0 で非推奨になっていたようだ。
どうも外部サービスに依存するのってどうなのよ?不安定になっちゃわない?っていう理由らしい。(それっぽい議論をしている Issue や PR をいくつか見つけた)

ということで、おそらく、教科書のままのコードでは画像は取得できないです。
対応方法は私も調べ中ですが、自分で用意した画像を参照するようにする感じですかね?

投稿2023/01/15 14:40

matsu_ura

総合スコア5

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問