1
2

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 3 years have passed since last update.

corcelを使ってdockerコンテナ間でlaravelからwordpressのDBに接続する

Last updated at Posted at 2019-12-21

##corcelの事前設定

下記を参考に進めていきます。
かなり巻いてます(師走だからね)。

まずはcorcelをインストール

composer require jgrossi/corcel

laravelのバージョンが5.4以下は下記を記載

config/app.php
'providers' => [
    /*
     * Package Service Providers...
     */
    Corcel\Laravel\CorcelServiceProvider::class,
]
php artisan vendor:publish --provider="Corcel\Laravel\CorcelServiceProvider"

databaseにwordpressの接続設定を記載。

config/database.php
    'wordpress' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'mydatabase',
        'username'  => 'admin',
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'wp_',
        'strict'    => false,
        'engine'    => null,
    ],

コネクションを'wordpress'へ変更

config/corcel.php
<?php

return [
(省略)
    'connection' => 'wordpress',
(省略)
];

WpPostモデルを作成します。

php artisan make:model WpPost
WpPost.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Corcel\Model\Post as Corcel;

class WpPost extends Corcel{
   protected $connection = 'wordpress';
}

##wordpressとlaravelのdockerの設定

事前に作成済みのwordpressのdocker-compose.ymlファイルの末尾に下記を記述

wordpress側docker-compose.yml
networks:
  default:
    external:
      name: new_network
laravel側docker-compose.yml
networks:
  default:
    external:
      name: new_network

createコマンドでnew_networkを作ります。

docker network create new_network

docker networkコマンドで確認します。

docker network ls
xxxxxxxxxx        my_network    bridge              local

dockerコンテナを立ち上げます。

docker-compose up

##laravel側のdatabase設定を修正

dockerコンテナ内のwordpress-mysqlにログインし、アドレスを確認します。

hostname -i
192.168.1.2

laravelのdatabase.phpの設定内容を変更します。
それぞれwordpressのwp-config.phpの設定を元に書き換えていってください。

config/database.php
    'wordpress' => [
        'driver'    => 'mysql',
        'host'      => 192.168.1.2,
        'database'  => 'mydatabase',
        'username'  => 'wordress_root',
        'password'  => 'wordpress_password',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'wp_',
        'strict'    => false,
        'engine'    => null,
    ],

##wordpressのDBに接続
laravelのdockerコンテナにログインし、wordpressのDBに接続します。

php artisan tinker
>>> App\WpPost::find(1)
=> App\WpPost {#5963
     ID: 1,
     post_author: 1,
     post_date: "2017-10-20 12:06:00",
     post_date_gmt: "2017-10-20 03:06:00",
     post_content: "WordPress へようこそ。これは最初の投稿です。編集もしくは削除してブログを始めてください !",
     post_title: "Hello world!",
     post_excerpt: "",
     post_status: "publish",
     comment_status: "open",
     ping_status: "open",
     post_password: "",
     post_name: "hello-world",
     to_ping: "",
     pinged: "",
     post_modified: "2017-10-20 12:06:00",
     post_modified_gmt: "2017-10-20 03:06:00",
     post_content_filtered: "",
     post_parent: 0,
     guid: "http://localhost:8888/?p=1",
     menu_order: 0,
     post_type: "post",
     post_mime_type: "",
     comment_count: 1,
     meta: Corcel\Model\Collection\MetaCollection {#5964
       all: [],
     },
   }

無事接続できました!!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?