Laravel v9 + Mysql v8 Set Up

# Laravel v9 MySQL v8 のセットアップ

### ディレクトリ
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/1a1e45e9-2e87-7369-9448-4f91ba7200c7.png)

# configフォルダのdatabase.php

```php:database.php
'default' => env('DB_CONNECTION', 'mysql'),
```

# .env
```.env
// mysql の設定を記述

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=todos
    DB_USERNAME=root
    DB_PASSWORD=
```

# データーベースを作成
phpMyAdminを使って、todosを作成
※テーブルの作成は不要
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/f06d9483-dcda-e1fb-fb41-0ff716d6246d.png)



# migrtionファイルを作成
```terminal:terminal
　php artisan make:migration create_todos_table
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/67ea3fae-ff02-a2da-b1f0-d967e5f6ef1f.png)

```php:create_todos_table.php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('todos', function (Blueprint $table) {
            $table->increments('id');
            $table->string('task');
            $table->boolean('completed');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('todos');
    }
};
```
# マイグレーション
```terminal:terminal
　php artisan migrate
```

# todosにテーブルが作成される
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/ab0ed43e-2aa9-4d4a-4f7d-9611bc4cf9ce.png)

# ダミーファイルを作成
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/ff02691e-db68-f31a-def9-5a437ef947ea.png)

```php:DatabaseSeeder.php
<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
       // コメントアウト
        \App\Models\User::factory(10)->create();

        // \App\Models\User::factory()->create([
        //     'name' => 'Test User',
        //     'email' => 'test@example.com',
        // ]);
    }
}
```

# seedの実行
```terminal:terminal
    php artisan db:seed
```

# テーブルの確認
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/bd15a209-cc76-d28c-c3de-4d807338a434.png)

# テーブルのリフレッシュ
```terminal:terminal
    php artisan migrate:refresh 
```

# seedのリフレッシュ
```terminal:terminal
    php artisan migrate:refresh --seed
```

# modelを作成
Todoのmodelを作成
```terminal:terminal
    php artisan make:model Todo
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/0128c9b2-a160-14e3-ca2b-85cf2583f2f6.png)

# Todoのseedの作成
```php:DatabaseSeeder.php
<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;

use App\Models\Todo;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        \App\Models\User::factory(10)->create();

        // \App\Models\User::factory()->create([
        //     'name' => 'Test User',
        //     'email' => 'test@example.com',
        // ]);

        //　マニュアル
        Todo::create([
            'task' => "Buy Car",
            "completed" => false,
        ]);

        // オート
        Todo::factory(10)->create();
    }
}
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/4df388d9-4d80-0819-e8e5-9b11ce4eed55.png)

# factoryファイルを作成
```terminal:terminal
    php artisan make:factory TodoFactory
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/58f438c2-39fc-4241-ca02-3d5f58f44884.png)

```php:TodoFactory.php
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Todo>
 */
class TodoFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            "task" => $this->faker->sentence(),
            "completed" => false,
        ];
    }
}
```

# 自動でseedを作成
```terminal:terminal
    php artisan migrate:refresh --seed
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/16bf8c90-5abd-5e98-36cf-d47eff0a7746.png)

# seederを作る場合
```terminal:terminal
    php artisan make:seeder SampleSeeder

    php artisan db:seed --class=SampleSeeder

    php aritsan migrate
```

# controllerを作る
```terminal:terminal
    php artisan make:controller StationaryController --resource --model=Stationary
```