LoginSignup
0
0

laravel 勉強日記

Posted at

laravel勉強日記2日目

新規アプリを作成するには作業ディレクトリにて、

curl -s "https://laravel.build/example-app?php=81" | bash
//example-appの部分はファイル名

開くためには以下のコマンド

cd example app
./vendor/bin/sail up -d

まず日本語で開発するためにファイルを置きます

/Users/x21024xx/Desktop/laravel_f/first-app/docker/8.2

ここに、my.cnfを置く
そして

/Users/x21024xx/Desktop/laravel_f/first-app/docker-compose.yml

のディレクトリにて

 volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './docker/8.2/my.cnf:/etc/my.cnf' //←を1行追加する

dbの作成を行う方法

sail artisan make:migration create_tweets_table

上のファイルは以下のディレクトリに作成される

database/migrations/yyyy_mm_dd_◯◯◯◯_create_tweets_table.php

中身はこんな感じ

.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('tweets', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tweets');
    }
};

マイグレーションクラスでは何をしてる?

関数のupメソッドでは新規に追加するテーブルや拡張するカラムなどのスキーマ(設計図)を指定する
downメソッドでは反対に戻す処理を行っている。

Up関数の記入例

public function up()
    {
        Schema::create('tweets', function (Blueprint $table) {
            $table->id();
            $table->string('content');
            $table->timestamps();
        });
    }

記入後の動き

Artisanコマンドからマイグレーションを実行する

./vendor/bin/sail artisan migrate

その後Mysqlにログインして作成を確認する、

./vendor/bin/sail mysql
mysql-> show tables from example_app; //ダメなら下
mysql-> show tables;
+------------------------+
| Tables_in_first_app    |
+------------------------+
| failed_jobs            |
| migrations             |
| password_reset_tokens  |
| personal_access_tokens |
| tweets                 |
| users                  |
+------------------------+
6 rows in set (0.01 sec)

mysql> show columns from tweets
    -> 
    -> ;
+------------+-----------------+------+-----+---------+----------------+
| Field      | Type            | Null | Key | Default | Extra          |
+------------+-----------------+------+-----+---------+----------------+
| id         | bigint unsigned | NO   | PRI | NULL    | auto_increment |
| content    | varchar(255)    | NO   |     | NULL    |                |
| created_at | timestamp       | YES  |     | NULL    |                |
| updated_at | timestamp       | YES  |     | NULL    |                |
+------------+-----------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

テーブル一覧に関数で指定した名前のテーブルがあれば成功。

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