Laravel で MariaDB のデータを表示

完成すると次のような表示が出ます
![laravel_jun0505.png](https://qiita-image-store.s3.amazonaws.com/0/179446/ba556ad1-297a-8470-5fc4-d5c7930bc41d.png)

次のページを参考にしました。
[Laravel入門[MVC]モデルを使ってデータベースからデータ取得・表示を行う](https://www.ritolab.com/entry/49)

MariaDB には、
>User: scott
>Password: tiger123
>データベース: city
>で cities というテーブルがあるとします。

ユーザーとデータベースの作成方法

```text
$ sudo mysql -uroot
mysql> create schema city;
mysql> create user 'scott'@'localhost' identified by 'tiger123';
mysql> grant all on city.* to 'scott'@'localhost';
mysql> flush privileges;
```

```text
$ mysql -uscott -ptiger123 city
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.1.33-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 [city]> select * from cities;
+-------+--------+------------+------------+
| id    | name   | population | date_mod   |
+-------+--------+------------+------------+
| t3329 | 新見   |     259718 | 1921-05-14 |
| t3323 | 津山   |     621597 | 1921-06-14 |
| t3324 | 玉野   |     952178 | 1921-04-30 |
| t3326 | 井原   |     213592 | 1921-01-10 |
| t3328 | 高梁   |     785231 | 1921-12-28 |
| t3321 | 岡山   |     397152 | 1921-10-12 |
| t3322 | 倉敷   |  892453100 | 2018-05-20 |
| t3327 | 総社   |     482967 | 1921-10-19 |
+-------+--------+------------+------------+
8 rows in set (0.00 sec)

MariaDB [city]>
```

<ol>
<li>プロジェクトの作成</li>

```bash
laravel new database
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/52827f78-5a05-bf1a-fd0a-bff844b50ef0.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/f20f9f42-07e9-eeff-ab9b-d879ac280f5e.png)


<li>.env の修正</li>

```text
DB_DATABASE=city
DB_USERNAME=scott
DB_PASSWORD=tiger123
```

<li>モデルクラスを作成</li>

```bash
php artisan make:model Cities
```

<li>app/Models/Cities.php の編集</li>

```php:app/Models/Cities.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class cities extends Model
{
  protected $table = 'cities';

  protected $guarded = array('id');

  public $timestamps = false;

  public function getData()
  {
    error_log ("*** getData() *** start ***");
  
    $data = DB::table($this->table)->get();

    error_log ("*** getData() *** end ***");

    return $data;
  }
}
```

<li>routes/web.php の最後に追記</li>

```text
Route::get('sample/model', 'App\Http\Controllers\SampleController@model');
```

<li>コントローラの作成</li>

```bash
php artisan make:controller SampleController
```

<li>app/Http/Controllers/SampleController.phpの編集</li>

```php:app/Http/Controllers/SampleController.php
<?php

namespace App\Http\Controllers;

use App\Models\Cities;

class SampleController extends Controller
{
    //
  public function model()
  {
    error_log ("*** model() *** start ***");
    // Citiesモデルのインスタンス化
    $md = new Cities();
    // データ取得
    $data = $md->getData();

    error_log ("*** model() *** end ***");
    
    // ビューを返す
    return view('sample.model', ['data' => $data]);
  }
}
```

<li>resources/views/sample/model.blade.php の作成</li>

```bash
mkdir resources/views/sample
```

```php:resources/views/sample/model.blade.php
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>model sample</title>
</head>
<body>
@php
 $keys=['id','name','population','date_mod'];
@endphp
  <table>
  <tr>
   @foreach($keys as $key)
   <th>{{ $key }}</th>
   @endforeach
  </tr>
    @foreach($data as $dd)
      <tr>
      @foreach($keys as $key)
       <td>{{ $dd->$key }}</td> 
      @endforeach
      </tr>
    @endforeach
  </table>
<p />
Jun/16/2018 AM 08:29<p />
</body>
</html>
```

<li>サーバーを起動</li>

```bash
php artisan serve --host 0.0.0.0
```

>ブラウザーでアクセスします。
http://localhost:8000/sample/model

冒頭の画面が表示されます。

## 確認したバージョン

```text
$ uname -a
Linux shimizu 6.2.0-32-generic #32-Ubuntu SMP PREEMPT_DYNAMIC Mon Aug 14 10:03:50 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

$ php --version
PHP 8.1.12-1ubuntu4.3 (cli) (built: Aug 17 2023 17:37:48) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.12-1ubuntu4.3, Copyright (c), by Zend Technologies

$ php artisan --version
Laravel Framework 10.22.0
```

## 次のエラーが出た場合の解決策

```text
PHP Warning:  PHP Startup: Unable to load dynamic library 'pdo_mysql'
```

```bash
sudo apt install php8-mysql
```