3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravel で MariaDB のデータを表示

Last updated at Posted at 2018-06-05

完成すると次のような表示が出ます
laravel_jun0505.png

次のページを参考にしました。
Laravel入門[MVC]モデルを使ってデータベースからデータ取得・表示を行う

MariaDB には、

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

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

$ 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;
$ 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]>
  1. プロジェクトの作成
  2. laravel new database
    

    image.png

    image.png

  3. .env の修正
  4. DB_DATABASE=city
    DB_USERNAME=scott
    DB_PASSWORD=tiger123
    
  5. モデルクラスを作成
  6. php artisan make:model Cities
    
  7. app/Models/Cities.php の編集
  8. 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;
      }
    }
    
  9. routes/web.php の最後に追記
  10. Route::get('sample/model', 'App\Http\Controllers\SampleController@model');
    
  11. コントローラの作成
  12. php artisan make:controller SampleController
    
  13. app/Http/Controllers/SampleController.phpの編集
  14. 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]);
      }
    }
    
  15. resources/views/sample/model.blade.php の作成
  16. mkdir resources/views/sample
    
    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>
    
  17. サーバーを起動
  18. php artisan serve --host 0.0.0.0
    

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

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

    確認したバージョン

    $ 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
    

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

    PHP Warning:  PHP Startup: Unable to load dynamic library 'pdo_mysql'
    
    sudo apt install php8-mysql
    
3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?