LoginSignup
10
8

Laravel で MariaDB の CRUD (その 1)

Last updated at Posted at 2018-06-13

次のページにほぼ従っています。
Laravel5.6入門 基本CRUD操作を体で覚える
相違点は、データベースを、MariaDB にしたことです。
Laravel Framework 10.37.3 を使いました。

Ubuntu で必要なソフトのインストール

sudo apt install php-mysql
  1. MariaDB の用意
  2. MariaDB に次のデータを用意します。

    User: scott
    Password: tiger123
    データベース: article

    接続の確認

    $ mysql -uscott -ptiger123 article
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 36
    Server version: 10.5.15-MariaDB-0ubuntu0.21.10.1 Ubuntu 21.10
    
    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 [article]> 
    
  3. プロジェクトの作成
  4. laravel new sample
    
  5. .env の設定
  6. もしも .env がない時は、

    cp .env.example .env
    

    .env のデータベースの部分を修正

    .env
    (省略)
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=article
    DB_USERNAME=scott
    DB_PASSWORD=tiger123
    (省略)
    

    もしも、
    APP_KEY=
    と空白の時は、

    php artisan key:generate
    

    次のように .env の APP_KEY が設定される
    APP_KEY=base64:xjG4svW8IkhyMg0NzCP+C4kVZGvs/KUO2XarVzJJdUg=

  7. モデルとマイグレーションを作成
  8. php artisan make:model Article -m
    

    -m をつけることで、
    database/migrations/2021_05_15_002753_create_articles_table.php が作成されます。

  9. マイグレーションファイルを編集
  10. 2行追加
    database/migrations/2021_05_15_002753_create_articles_table.php
    // 略
     public function up()
        {
            Schema::create('articles', function (Blueprint $table) {
                $table->id();
           $table->string('title');  // 追加
           $table->text('body');  // 追加
                $table->timestamps();
            });
    // 略
    
  11. app/Providers/AppServiceProvider.php の修正
  12. 2行追加 >この修正は、MariaDB との接続に必要です。
    app/Providers/AppServiceProvider.php
    <?php
    
    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    use Illuminate\Support\Facades\Schema;  // 追加
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot()
        {
            Schema::defaultStringLength(191);   // 追加
        }
    // 略
    
  13. マイグレーションファイルの内容をデータベースに反映
  14. php artisan migrate
    

    MariaDB は次のようになります。

    MariaDB [article]> show tables;
    +-------------------+
    | Tables_in_article |
    +-------------------+
    | articles          |
    | migrations        |
    | password_resets   |
    | users             |
    +-------------------+
    4 rows in set (0.03 sec)
    
    MariaDB [article]>
    
  15. コントローラーの作成
  16. php artisan make:controller ArticlesController -r
    
  17. ルーティングを定義
  18. >最後に一行を追加
    routes/web.php
    // 略
    Route::resource('articles', 'App\Http\Controllers\ArticlesController');
    
  19. MariaDB のテーブル articles にデータを入れる。
  20. 次のような SQL で入れます。
    insert into articles set id=10, title="aaa", body="こんにちは";
    insert into articles set id=11, title="bbb", body="おはよう";
    insert into articles set id=12, title="ccc", body="今晩は";
    

    MariaDB の状態

    MariaDB [article]> select * from articles;
    +----+------------+------------+-------+-----------------+
    | id | created_at | updated_at | title | body            |
    +----+------------+------------+-------+-----------------+
    | 10 | NULL       | NULL       | aaa   | こんにちは      |
    | 11 | NULL       | NULL       | bbb   | おはよう        |
    | 12 | NULL       | NULL       | ccc   | 今晩は          |
    +----+------------+------------+-------+-----------------+
    3 rows in set (0.000 sec)
    
    MariaDB [article]>
    
  21. 一覧を表示するように、コントローラーを作成
  22. app/Http/Controllers/ArticlesController.php
    // 略
    use App\Models\Article;
    // 略
        public function index()
        {
          $articles = Article::all();
          return $articles;
        }
    // 略
    
  23. サーバーを起動
  24. php artisan serve
    
  25. curl でアクセスして JSON でデータの一覧が返ってくることを確認
  26. curl http://127.0.0.1:8000/articles | jq .
    
    [
      {
        "id": 10,
        "title": "aaa",
        "body": "こんにちは",
        "created_at": null,
        "updated_at": null
      },
      {
        "id": 11,
        "title": "bbb",
        "body": "おはよう",
        "created_at": null,
        "updated_at": null
      },
      {
        "id": 12,
        "title": "ccc",
        "body": "今晩は",
        "created_at": null,
        "updated_at": null
      }
    ]
    

Laravel のバージョンの確認

$ php artisan --version
Laravel Framework 10.37.3

次のページ

Laravel で MariaDB の CRUD (その 2)

10
8
1

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
10
8