0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravel v9 + Mysql v8 Set Up

Last updated at Posted at 2022-10-27

Laravel v9 MySQL v8 のセットアップ

ディレクトリ

image.png

configフォルダのdatabase.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

migrtionファイルを作成

terminal
 php artisan make:migration create_todos_table

image.png

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
 php artisan migrate

todosにテーブルが作成される

image.png

ダミーファイルを作成

image.png

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
    php artisan db:seed

テーブルの確認

image.png

テーブルのリフレッシュ

terminal
    php artisan migrate:refresh 

seedのリフレッシュ

terminal
    php artisan migrate:refresh --seed

modelを作成

Todoのmodelを作成

terminal
    php artisan make:model Todo

image.png

Todoのseedの作成

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

factoryファイルを作成

terminal
    php artisan make:factory TodoFactory

image.png

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
    php artisan migrate:refresh --seed

image.png

seederを作る場合

terminal
    php artisan make:seeder SampleSeeder

    php artisan db:seed --class=SampleSeeder

    php aritsan migrate

controllerを作る

terminal
    php artisan make:controller StationaryController --resource --model=Stationary
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?