7
3

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 で MongoDB のデータを表示

Last updated at Posted at 2018-06-12

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

次のページを参考にしました。
2018.02.09 Laravel5.5でMongoDBを使ったWebサイト作成

MongoDB のデータの状態

$ mongosh
(省略)
test> use testDB;
switched to db testDB
testDB> db.post.find()
[
  {
    _id: ObjectId("609ef895eb0964e1af2f650c"),
    body: 'おはよう',
    date: '2018-1-10'
  },
  {
    _id: ObjectId("609ef89deb0964e1af2f650d"),
    body: 'こんにちは',
    date: '2018-2-20'
  },
  {
    _id: ObjectId("609ef8a4eb0964e1af2f650e"),
    body: '今日は雨が降ってます。',
    date: '2018-3-15'
  }
]
testDB>

上記のデータを、mongosh で作成する方法

use testDB;
db.post.insertOne({"body": "おはよう","date": "2018-1-10"});
db.post.insertOne({"body": "こんにちは","date": "2018-2-20"});
db.post.insertOne({"body": "今日は雨が降ってます。","date": "2018-3-15"});
  1. プロジェクトの作成
laravel new proj01
cd proj01
composer require "jenssegers/mongodb"
  1. config/app.php の編集
'providers' => [
// 略
        Jenssegers\Mongodb\MongodbServiceProvider::class,
 ],

    'aliases' => [
// 略
         'Moloquent' => Jenssegers\Mongodb\Eloquent\Model::class,
 ],
  1. config/database.php の編集
    'default' => env('DB_CONNECTION', 'mongodb'),
    // 略
    'connections' => [
        'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', 27017),
            'database' => env('DB_DATABASE', 'testDB'),
        ],
    ],

認証がある場合は、次を加えます。

'username' => env('DB_USERNAME','scott'),
'password' => env('DB_PASSWORD','tiger123'),


4) .env の編集

```text
#DB_CONNECTION=mysql
#DB_HOST=127.0.0.1
#DB_PORT=3306
#DB_DATABASE=homestead
#DB_USERNAME=homestead
#DB_PASSWORD=secret
DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=testDB
  1. キャッシュのクリア
php artisan cache:clear
  1. routes/web.php の編集
routes/web.php
<?php
Route::get('/', 'App\Http\Controllers\TestController@index');
  1. app/Http/Controllers/TestController.php の作成
php artisan make:controller TestController

app/Http/Controllers/TestController.php を編集

app/Http/Controllers/TestController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class TestController extends Controller
{
    static function index() {
        $posts = Post::orderBy('_id', 'desc')->get();
        return view('index', ['posts' => $posts]);
    }
}
  1. app/Post.php の作成

MongoDB の データベース testDB のコレクションが post です。

app/Post.php
<?php
namespace App;
class Post extends \Moloquent
{
    protected $collection = 'post';
}
  1. resources/views/index.blade.php の作成
resources/views/index.blade.php
<div>全部で{{ count($posts) }}件です</div>
<ul>
  @foreach($posts as $post)
    <li>{{ $post['date'] . ' | ' . $post['body'] }}</li>
  @endforeach
</ul>
  1. サーバーの起動
php artisan serve
  1. クライアントからアクセス

http://localhost:8000

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

$ 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
7
3
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
7
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?