底辺インフラエンジニアのおじさんがLaravelをいじってみる

2021/12/16に公開

底辺インフラエンジニアのおじさんがLaravelをいじってみる

前提

  • laravel6を使用
  • Cent7系
  • php7.3 インストール済
  • DBはmaridb(or mysql) インストール済
  • nginxにはlaravelのパスで80ポートで受け付ける設定済
composerのインストーラーをインストールする
composer global require laravel/installer

プロジェクト作成

cd /var/www
composer create-project --prefer-dist laravel/laravel projectname "6.*"

以下ディレクトリのパーミッションをnginxにする

/var/www/proectname/storage
chown -R nginx:nginx *

nginxを変更&再起動

systemctl restart nginx

確認

http://localhost

artisanとか

  • ルーティング確認
# php artisan route:list
+--------+----------+--------------+------+---------+--------------+
| Domain | Method   | URI          | Name | Action  | Middleware   |
+--------+----------+--------------+------+---------+--------------+
|        | GET|HEAD | /            |      | Closure | web          |
|        | GET|HEAD | api/user     |      | Closure | api,auth:api |
|        | GET|HEAD | hello/{name} |      | Closure | web          |
+--------+----------+--------------+------+---------+-------------
  • tinker

デバッグ用なのであとでちゃんと確認

# php artisan tinker
Psy Shell v0.10.12 (PHP 7.3.29 ― cli) by Justin Hileman
>>>
>>> echo "aa";
aa?
=> null
>>>
>>> $name="larabel"
=> "larabel"

データベースをいじる

https://readouble.com/laravel/6.x/ja/migrations.html

  • まずDB,ユーザ作成
MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> grant all privileges on testdb.* to 'testuser'@'localhost' identified by 'XXX';
Query OK, 0 rows affected (0.017 sec)

# mysql -u testuser  -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 10.4.22-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| testdb             |
+--------------------+
2 rows in set (0.001 sec)
  • .envに接続情報を書く
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testdb
DB_USERNAME=testuser
DB_PASSWORD=xxxxxx
  • マイグレーションファイルの作成
# php artisan make:migration create_users_table --create=users
Created Migration: 2021_12_15_040559_create_users_table
合計 4.0K
-rw-r--r--. 1 root root 589 12月 15 13:05 2021_12_15_040559_create_users_table.php
# cat database/migrations/2021_12_15_040559_create_users_table.php
<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
  • マイグレーション実行
# php artisan migrate
Migration table created successfully.
Migrating: 2021_12_15_040559_create_users_table
Migrated:  2021_12_15_040559_create_users_table (0.04 seconds```

MariaDB [testdb]> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| migrations       |
| users            |
+------------------+

seeds

データを生成

cat UsersTableSeeder
        $users = [
            ['name' => 'あああ',
             'airline' => 'いいい'],
            ['name' => 'かかか',
             'airline' => 'いいい']

            ];
# php artisan db:seed
Seeding: UsersTableSeeder
Seeded:  UsersTableSeeder (0.2 seconds)
Database seeding completed succe
MariaDB [testdb]> select * from users;
+----+-----------------+-----------+---------------------+---------------------+
| id | name            | airline   | created_at          | updated_at          |
+----+-----------------+-----------+---------------------+---------------------+
|  1 | あああ          | いいい    | 2021-12-16 03:07:38 | 2021-12-16 03:07:38 |
|  2 | かかか      | いいい    | 2021-12-16 03:07:38 | 2021-12-16 03:07:38 |
+----+-----------------+-----------+---------------------+---------------------+

Discussion