LoginSignup
1
2

More than 5 years have passed since last update.

Laradoc + Laravel + mongoDB (jenssegers/mongodb)

Last updated at Posted at 2018-12-23

はじめに

Laradoc に jenssegers/mongodb をサブデータベースとして導入する手順です。
多分メインデータベースとしてもマイグレーションすればいけるとは思う。
https://github.com/jenssegers/laravel-mongodb

PHP環境にMongoDBのドライバを追加する

[ec2-user@ip-xx-x-x-xxx laradock]$ docker exec -ti --env COLUMNS=`tput cols` --env LINES=`tput lines` --user=root -it laradock_php-fpm_1 /bin/bash
root@051ed95058d6:/var/www# pecl install mongodb
downloading mongodb-1.5.3.tgz ...
Starting to download mongodb-1.5.3.tgz (1,059,580 bytes)
.................................................................................................................................................................................................................done: 1,059,580 bytes
426 source files, building
running: phpize
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718
building in /tmp/pear/temp/pear-build-defaultuserj0l8Jn/mongodb-1.5.3
running: /tmp/pear/temp/mongodb/configure --with-php-config=/usr/local/bin/php-config
checking for grep that handles long lines and -e... /bin/grep
 :
 :
 :
 :


Build process completed successfully
Installing '/usr/local/lib/php/extensions/no-debug-non-zts-20170718/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.5.3
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongodb.so" to php.ini
root@051ed95058d6:/var/www#
root@051ed95058d6:/var/www# cat > /usr/local/etc/php/conf.d/docker-php-ext-mongo.ini
extension=mongodb.so
^d

PHP環境を再起動

root@051ed95058d6:/var/www# exit
[ec2-user@ip-xx-x-x-xxx laradock]$ docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                      NAMES
0f87f3ebfe8b        starefossen/pgrouting   "docker-entrypoint.s…"   11 days ago         Up 25 hours         0.0.0.0:5432->5432/tcp                     stupefied_keller
39d295818587        laradock_caddy          "sh -c '/start.sh & …"   13 days ago         Up 25 hours         0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   laradock_caddy_1
051ed95058d6        laradock_php-fpm        "docker-php-entrypoi…"   13 days ago         Up 25 hours         9000/tcp                                   laradock_php-fpm_1
a2a15ac50d96        laradock_workspace      "/sbin/my_init"          13 days ago         Up 25 hours         0.0.0.0:2222->22/tcp                       laradock_workspace_1
7f96cde07782        docker:dind             "dockerd-entrypoint.…"   13 days ago         Up 25 hours         2375/tcp                                   laradock_docker-in-docker_1
[ec2-user@ip-xx-x-x-xxx laradock]$ docker restart laradock_php-fpm_1
laradock_php-fpm_1

Workspace環境にもMongoDBドライバを追加

[ec2-user@ip-xx-x-x-xxx laradock]$ docker exec -ti --env COLUMNS=`tput cols` --env LINES=`tput lines` --user=root -it laradock_workspace_1 /bin/bash
root@a2a15ac50d96:/var/www# pecl install mongodb
downloading mongodb-1.5.3.tgz ...
Starting to download mongodb-1.5.3.tgz (1,059,580 bytes)
.................................................................................................................................................................................................................done: 1,059,580 bytes
426 source files, building
running: phpize
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718
building in /tmp/pear/temp/pear-build-defaultuserR0YWJX/mongodb-1.5.3
running: /tmp/pear/temp/mongodb/configure --with-php-config=/usr/bin/php-config
checking for grep that handles long lines and -e... /bin/grep
 :
 :
 :
 :

Build process completed successfully
Installing '/usr/lib/php/20170718/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.5.3
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongodb.so" to php.ini
root@a2a15ac50d96:/var/www#
[ec2-user@ip-xx-x-x-xxx laradock]$ docker exec -ti --env COLUMNS=`tput cols` --env LINES=`tput lines` --user=root -it laradock_workspace_1 /bin/bash
root@a2a15ac50d96:/var/www# cat > /etc/php/7.2/cli/conf.d/20-mongo.ini
extension=mongodb.so
^d

LaravelにmongoDB拡張をインストール

[ec2-user@ip-xx-x-x-xxx laradock]$ docker exec -ti --env COLUMNS=`tput cols` --env LINES=`tput lines` --user=laradock -it laradock_workspace_1 /bin/bash
laradock@a2a15ac50d96:/var/www$ composer require "jenssegers/mongodb=^3.4.4"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
  - Installing mongodb/mongodb (1.4.2): Downloading (100%)
  - Installing jenssegers/mongodb (v3.4.5): Downloading (100%)
jenssegers/mongodb suggests installing jenssegers/mongodb-session (Add MongoDB session support to Laravel-MongoDB)
jenssegers/mongodb suggests installing jenssegers/mongodb-sentry (Add Sentry support to Laravel-MongoDB)
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: bavix/laravel-admin-leaflet
Discovered Package: beyondcode/laravel-dump-server
Discovered Package: encore/laravel-admin
Discovered Package: fideloper/proxy
Discovered Package: jenssegers/mongodb
Discovered Package: laravel/nexmo-notification-channel
Discovered Package: laravel/slack-notification-channel
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
laradock@a2a15ac50d96:/var/www$

config/app.php

    'providers' => [
        // MongoDB
        Jenssegers\Mongodb\MongodbServiceProvider::class,
        :
        :
    ],
    'aliases' => [
        // MongoDB
        'Moloquent' => Jenssegers\Mongodb\Eloquent\Model::class,
        :
        :
    ],

config/database.php

    'connections' => [
        'mongodb' => [
            'driver'   => 'mongodb',
            'host'     => 'xxx.xx.xx.xxx',
            'port'     => 27017,
            'database' => 'xxxx',
        ],
  • 忘れずに MongoDB サーバのポートに穴を開けておきましょう

Model

<?php
namespace App\Model;

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class XXXXXXModel extends Eloquent
{
        protected $connection = 'mongodb';
        protected $collection = 'xxxxx_collection';
        public $timestamps = false;
}

ObjectId

mongoDBでは頻繁にObjectIdを使用するが、

$acc = Account::where('mail','=','hoge@hoge.com'))->first();

ここでの $acc->_id は ObjectId クラスじゃなく、文字列型のようです。
逆に、クエリビルダで ObjectId を検索する場合は、

$grid->model()->where('account_id','=',new \MongoDB\BSON\ObjectId('59a8358ab92fc626f2295340'));

のように \MongoDB\BSON\ObjectId クラスを作ってあげないとヒットしません。

1
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
1
2