Laravel 10 入門トップページ


目次

  1. 概要
  2. プロジェクトの作成と初期設定
  3. 一対多のリレーションシップ
  4. 多対多のリレーションシップ
  5. ビューを使った表示
  6. 学部の情報を表示・変更する
  7. キャンパスの情報を表示・変更・削除する
  8. 学生の情報を表示・変更・削除する

Laravel でリレーションシップを使いこなす

プロジェクトの作成と初期設定

プロジェクトの作成

今回は laravelRelationship というプロジェクトを新規に作成します.

vagrant@ubuntu2204 laravel $ pwd ⏎
/home/vagrant/Documents/laravel
vagrant@ubuntu2204 laravel $ ls ⏎
comment_app  composer.phar
vagrant@ubuntu2204 laravel $ php composer.phar create-project --prefer-dist laravel/laravel laravelRelationship ⏎
Creating a "laravel/laravel" project at "./laravelRelationship"
Installing laravel/laravel (v10.2.6)
  - Installing laravel/laravel (v10.2.6): Extracting archive
Created project in /home/vagrant/Documents/laravel/laravelRelationship

(中略)

43 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   INFO  Discovering packages.

  laravel/sail ............................... DONE
  laravel/sanctum ............................ DONE
  laravel/tinker ............................. DONE
  nesbot/carbon .............................. DONE
  nunomaduro/collision ....................... DONE
  nunomaduro/termwind ........................ DONE
  spatie/laravel-ignition .................... DONE

82 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force

   INFO  No publishable resources for tag [laravel-assets].

No security vulnerability advisories found.
> @php artisan key:generate --ansi

   INFO  Application key set successfully.

vagrant@ubuntu2204 laravel $

Git によるバージョン管理を行うのであれば初期設定を行ってコミットしておきます.

vagrant@ubuntu2204 laravel $ cd laravelRelationship ⏎
vagrant@ubuntu2204 laravelRelationship $ ls ⏎
README.md  bootstrap      config        phpunit.xml  routes   vendor
app        composer.json  database      public       storage  vite.config.js
artisan    composer.lock  package.json  resources    tests
vagrant@ubuntu2204 laravelRelationship $ git init ⏎
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: 	git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: 	git branch -m <name>
Initialized empty Git repository in /home/vagrant/Documents/laravel/laravelRelationship/.git/
vagrant@ubuntu2204 laravelRelationship $ git add . ⏎
vagrant@ubuntu2204 laravelRelationship $ git commit -m'initial commit' ⏎
[master (root-commit) 46d3c7c] initial commit
 79 files changed, 11104 insertions(+)
 create mode 100644 .editorconfig

(中略)

 create mode 100644 vite.config.js
vagrant@ubuntu2204 laravelRelationship $ git log ⏎
commit 46d3c7c4e8117e48ccb679e2b2f1dfeb433153a1 (HEAD -> master)
Author: Koichiro Rinsaka <xxxxxxxx@yy.zzzzzzzz.ac.jp>
Date:   Tue Oct 17 13:04:03 2023 +0900

    initial commit
vagrant@ubuntu2204 laravelRelationship $

目次に戻る

.env に初期設定を記載する

.env ファイルを修正して,データベースの設定を行います.今回はデータベースに SQLite を使うので,その設定に変更します.もちろん MySQL を利用しても構いません.

.env(抜粋)DB_CONNECTION=sqlite
# DB_CONNECTION=mysql
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=laravel
# DB_USERNAME=root
# DB_PASSWORD=

目次に戻る

タイムゾーンと言語の設定

プロジェクトの設定ファイル config/app.php を編集し,タイムゾーンと言語(およびフェイカの言語)の設定を行います.

    /*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => 'Asia/Tokyo',

    /*
    |--------------------------------------------------------------------------
    | Application Locale Configuration
    |--------------------------------------------------------------------------
    |
    | The application locale determines the default locale that will be used
    | by the translation service provider. You are free to set this value
    | to any of the locales which will be supported by the application.
    |
    */

    'locale' => 'ja',

    /*
    |--------------------------------------------------------------------------
    | Application Fallback Locale
    |--------------------------------------------------------------------------
    |
    | The fallback locale determines the locale to use when the current one
    | is not available. You may change the value to correspond to any of
    | the language folders that are provided through your application.
    |
    */

    'fallback_locale' => 'en',

    /*
    |--------------------------------------------------------------------------
    | Faker Locale
    |--------------------------------------------------------------------------
    |
    | This locale will be used by the Faker PHP library when generating fake
    | data for your database seeds. For example, this will be used to get
    | localized telephone numbers, street address information and more.
    |
    */

    'faker_locale' => 'ja_JP',

目次に戻る

空のデータベースファイルを作成する

SQLite のデータベースファイル database/database.sqlite を作成します.

vagrant@ubuntu2204 laravelRelationship $ ls ⏎
README.md  bootstrap      config        phpunit.xml  routes   vendor
app        composer.json  database      public       storage  vite.config.js
artisan    composer.lock  package.json  resources    tests
vagrant@ubuntu2204 laravelRelationship $ cd database ⏎
vagrant@ubuntu2204 database $ ls ⏎
factories  migrations  seeders
vagrant@ubuntu2204 database $ touch database.sqlite ⏎
vagrant@ubuntu2204 database $ ls ⏎
database.sqlite  factories  migrations  seeders
vagrant@ubuntu2204 database $ cd .. ⏎
vagrant@ubuntu2204 laravelRelationship $

目次に戻る

Web サーバを起動する

テスト用の Web サーバを起動します.リモートサーバ (ubuntu) には IP アドレスは 192.168.56.101 が設定されているものとします.IP アドレスを調べる方法はここを参照してください.またポート番号は 8000 番とします.

vagrant@ubuntu2204 laravelRelationship $ php artisan serve --host=192.168.56.101 --port=8000

   INFO  Server running on [http://192.168.56.101:8000].

  Press Ctrl+C to stop the server

クライアント PC の Web ブラウザからトップページ (http://192.168.56.101:8000) にアクセスします.

laravel10-2023-relationship-01.png

Web サーバを停止するには Ctrl + C を押します.

vagrant@ubuntu2204 laravelRelationship $ php artisan serve --host=192.168.56.101 --port=8000

   INFO  Server running on [http://192.168.56.101:8000].

  Press Ctrl+C to stop the server

  2023-10-17 13:15:17 ........................... ~ 0s
^C
vagrant@ubuntu2204 laravelRelationship $

目次に戻る