Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What are the problem?

posted at

updated at

Laravel で MariaDB のデータを表示

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

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

MariaDB には、

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

$ 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) プロジェクトの作成

laravel new database

2) .env の修正

DB_DATABASE=city
DB_USERNAME=scott
DB_PASSWORD=tiger123

3) モデルクラスを作成

php artisan make:model Cities

4) app/Models/Cities.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()
  {
    $data = DB::table($this->table)->get();

    return $data;
  }
}

5) routes/web.php の最後に追記

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

6) コントローラの作成

php artisan make:controller SampleController

7) app/Http/Controllers/SampleController.phpの編集

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

namespace App\Http\Controllers;

use App\Models\Cities;

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

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

8) resources/views/sample/model.blade.php の作成

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>

9) サーバーを起動して、

php artisan serve

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

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

次の環境で確認しました。

$ uname -a
Linux iwata 5.11.0-16-generic #17-Ubuntu SMP Wed Apr 14 20:12:43 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

$ php --version
PHP 7.4.16 (cli) (built: Mar 23 2021 16:15:03) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies

$ php artisan --version
Laravel Framework 8.40.0
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
2
Help us understand the problem. What are the problem?