次のページを参考にしました。
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.13.0-27-generic #29-Ubuntu SMP Wed Jan 12 17:36:47 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
$ php --version
PHP 8.0.8 (cli) (built: Oct 26 2021 11:42:42) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.8, Copyright (c) Zend Technologies
with Zend OPcache v8.0.8, Copyright (c), by Zend Technologies
$ php artisan --version
Laravel Framework 8.82.0
次のエラーが出た場合の解決策
PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_mysql'
sudo apt install php8-mysql