Laravel에서 MariaDB 데이터 보기

10031 단어 PHP8mariadb라라벨
완성하면 다음과 같은 표시가 나옵니다.


다음 페이지를 참고했습니다.
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

좋은 웹페이지 즐겨찾기