渋谷で働くエンジニア福さんのLaravel入門でつまづいたので備忘録まとめます。
YouTube動画ではおそらくMacOSでLaravelのバージョンが5.8ですが、環境等が違っていました。
開発環境
OS:Windows10
PHP:PHP 7.4.20
Laravel:Laravel Framework 8.48.1
MySQL:MySQL Server 8.0 (Ver 8.0.23 for Win64 on x86_64 (MySQL Community Server - GPL))
Node.js:14.17.20
npm:6.14.13
もくじ
- Models/Blog.phpの記述
- BlogsTableSeeder.phpの修正
- BlogFactory.phpの修正
- DatabaseSeeder.phpの修正
- no image
- no image
Models/Blog.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Blog extends Model
{
use HasFactory;
// テーブル名
protected $table = 'blogs';
// 可変項目
protected $fillable =
[
'title',
'content'
];
}
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Blog extends Model
{
use HasFactory;
// テーブル名
protected $table = 'blogs';
// 可変項目
protected $fillable =
[
'title',
'content'
];
}
BlogsTableSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\Blog;
class BlogsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\Models\Blog::class)->create();
}
}
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\Blog;
class BlogsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\Models\Blog::class)->create();
}
}
動画では“factory(Blog::class, 15)->create();”となっていたが、修正。
BlogFactory.php
<?php
namespace Database\Factories;
use App\Models\Blog;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class BlogFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Blog::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'title' => $this->faker->word,
'content' => $this->faker->realText
];
}
}
namespace Database\Factories;
use App\Models\Blog;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class BlogFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Blog::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'title' => $this->faker->word,
'content' => $this->faker->realText
];
}
}
こちらも“'title' => $Faker->word,”と“'content' => $Faker->realText,”と書かれていたが、$thisをつける書き方に修正
DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
\App\Models\Blog::factory(15)->create();
}
}
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
\App\Models\Blog::factory(15)->create();
}
}
こちらも“$this->call(BlogsTableSeeder::class);”と書かれていたが、修正
出典:渋谷で働くエンジニア福の「実践で学ぶプログラミング入門」
( https://www.youtube.com/channel/UCqzJi2o7bAPtRD3tTFvi7zA)
【このカテゴリーの最新記事】