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

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

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

Laravel 6は、オープンソースなPHPのフレームワーク。Webアプリケーションの開発に適しており、バージョン6はLTSです。5.8での向上に加えて、セマンティックバージョニングの採用やLaravel Vaporとのコンパチビリティなどが変更されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

461閲覧

seeder実行時エラーが表示される。

ishidakouiki

総合スコア73

Laravel 6

Laravel 6は、オープンソースなPHPのフレームワーク。Webアプリケーションの開発に適しており、バージョン6はLTSです。5.8での向上に加えて、セマンティックバージョニングの採用やLaravel Vaporとのコンパチビリティなどが変更されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2022/08/04 16:08

・やりたい事。
作成した、moviesテーブルとgenresテーブルにseederを実行したい。

・問題点
映画登録サイトを作成中でseederを実行したことろ、

Unable to locate factory with name [default] [App\Genre].

とエラーが表示されました。

・試した事。
① composer dump-autoload
結果→エラーなし。

②作成した、GenreFactroy.phpファイルの

$factory->define(Model::class, function (Faker $faker) { から $factory->define(Genres::class, function (Faker $faker) {

に変更。

結果→変化なし。

・テーブル構成

usesテーブル

2022_08_01_000000_create_users_table.php

1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7class CreateUsersTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('users', function (Blueprint $table) { 17 $table->bigIncrements('id'); 18 $table->string('name'); 19 $table->string('email')->unique(); 20 $table->timestamp('email_verified_at')->nullable(); 21 $table->string('password'); 22 $table->string('image_path')->nullable(); 23 $table->string('text')->nullable(); 24 $table->rememberToken(); 25 $table->timestamps(); 26 }); 27 } 28 29 /** 30 * Reverse the migrations. 31 * 32 * @return void 33 */ 34 public function down() 35 { 36 Schema::dropIfExists('users'); 37 } 38} 39

moviesテーブル

2022_08_01_214020_create_movies_table.php

1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7class CreateMoviesTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('movies', function (Blueprint $table) { 17 $table->bigIncrements('id'); 18 $table->string('image_path'); 19 $table->string('title'); 20 $table->string('director'); 21 $table->string('release'); 22 $table->string('evaluation'); 23 $table->string('viewing'); 24 $table->string('review'); 25 $table->unsignedBigInteger('user_id'); 26 $table->timestamps(); 27 28 //外部キー制約、user情報が削除されたら映画テーブルも削除される 29 $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 30 }); 31 } 32 33 /** 34 * Reverse the migrations. 35 * 36 * @return void 37 */ 38 public function down() 39 { 40 Schema::dropIfExists('movies'); 41 } 42} 43

genresテーブル

2022_07_31_214216_create_genres_table.php

1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7class CreateGenresTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('genres', function (Blueprint $table) { 17 $table->bigIncrements('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('genres'); 31 } 32} 33

・シーダーファイル

DatabaseSeeder.php

1<?php 2 3use Illuminate\Database\Seeder; 4 5class DatabaseSeeder extends Seeder 6{ 7 /** 8 * Seed the application's database. 9 * 10 * @return void 11 */ 12 public function run() 13 { 14 $this->call(GenresTableSeeder::class); 15 $this->call(MoviesTableSeeder::class); 16 $this->call(UsersTableSeeder::class); 17 } 18} 19

GenresTableSeeder.php

1<?php 2 3use Illuminate\Database\Seeder; 4use Illuminate\Support\Facades\DB; 5use App\Genre; 6 7class GenresTableSeeder extends Seeder 8{ 9 /** 10 * Run the database seeds. 11 * 12 * @return void 13 */ 14 public function run() 15 { 16 $genres = factory(App\Genre::class, 10)->create(); 17 } 18}

MoviesTableSeeder.php

1<?php 2 3use Illuminate\Database\Seeder; 4 5class MoviesTableSeeder extends Seeder 6{ 7 /** 8 * Run the database seeds. 9 * 10 * @return void 11 */ 12 public function run() 13 { 14 $movies = factory(App\Movie::class, 1)->create(); 15 } 16} 17

UsersTableSeeder.php

1<?php 2 3use Illuminate\Database\Seeder; 4use App\User; 5 6class UsersTableSeeder extends Seeder 7{ 8 /** 9 * Run the database seeds. 10 * 11 * @return void 12 */ 13 public function run() 14 { 15 factory(User::class, 10)->create(); 16 } 17} 18

・factoriesファイル

GenreFactroy.php

1<?php 2 3/** @var \Illuminate\Database\Eloquent\Factory $factory */ 4 5use App\Model; 6use Faker\Generator as Faker; 7 8$factory->define(Genres::class, function (Faker $faker) { 9 return [ 10 'name' => $faker->アニメ, 11 ]; 12 [ 13 'name' => $faker->ドラマ, 14 ]; 15}); 16

MovieFactory.php

1<?php 2 3/** @var \Illuminate\Database\Eloquent\Factory $factory */ 4 5use App\Model; 6use Faker\Generator as Faker; 7 8$factory->define(Movie::class, function (Faker $faker) { 9 return [ 10 'image_path' => $faker->テスト, 11 'title' => $faker->テスト, 12 'director' => $faker->テスト, 13 'release' => $faker->テスト, 14 'evaluation' => $faker->テスト, 15 'viewing' => $faker->テスト, 16 'review' => $faker->テスト, 17 'user_id' => $faker->テスト, 18 ]; 19}); 20

・モデルファイル

User.php

1<?php 2 3namespace App; 4 5use Illuminate\Contracts\Auth\MustVerifyEmail; 6use Illuminate\Foundation\Auth\User as Authenticatable; 7use Illuminate\Notifications\Notifiable; 8 9class User extends Authenticatable 10{ 11 use Notifiable; 12 13 /** 14 * The attributes that are mass assignable. 15 * 16 * @var array 17 */ 18 protected $fillable = [ 19 'name', 'email', 'password', 20 ]; 21 22 /** 23 * The attributes that should be hidden for arrays. 24 * 25 * @var array 26 */ 27 protected $hidden = [ 28 'password', 'remember_token', 29 ]; 30 31 public function movies() 32 { 33 return $this->hasMany(Movie::class); 34 } 35 36 /** 37 * The attributes that should be cast to native types. 38 * 39 * @var array 40 */ 41 protected $casts = [ 42 'email_verified_at' => 'datetime', 43 ]; 44} 45

Movie.php

1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Movie extends Model 8{ 9 protected $fillable = ['image_path','title','director','release','evaluation','viewing','review','user_id']; 10 11 public function user() 12 { 13 return $this->belongsTo(User::class); 14 } 15} 16 17``````Genre.php 18<?php 19 20namespace App; 21 22use Illuminate\Database\Eloquent\Model; 23 24class Genre extends Model 25{ 26 protected $fillable = ['name']; 27} 28

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

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

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

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

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

phper.k

2022/08/05 05:18

Factory をちゃんと作ってないだけでは?
ishidakouiki

2022/08/05 08:53

Factoryはseederを実行したい、 GenreとMovieのファイルを作成しているのですが、 記載の不備等ありますでしょうか?
phper.k

2022/08/05 08:56

なにかって、 $faker->テスト, こんな書き方ないですよ
ishidakouiki

2022/08/06 10:29

ファクトリーファイルるとseederファイルの記述を変更したら無事実装できました! ありがとうございます!
guest

回答1

0

自己解決

ファクトリーファイルるとseederファイルの記述を変更。

投稿2022/08/06 10:29

ishidakouiki

総合スコア73

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問