1 year ago

Two database connections defined but not working

Posted 1 year ago by ChrisF79

I have two databases defined so I can have a Wordpress GUI to use as a CMS yet build the output using Laravel over my app.

Anyway, my .env looks like this:

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', ''),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        'wordpress' => [ // for WordPress database (used by Corcel)
            'driver'    => 'mysql',
            'host' => env('DB_HOST', ''),
            'port' => env('DB_PORT', '3306'),
            'database' => env('WP_DB_DATABASE', 'forge'),
            'username' => env('WP_DB_USERNAME', 'forge'),
            'password' => env('WP_DB_PASSWORD', ''),
            'prefix'    => 'wp_',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'strict'    => true,
            'engine'    => null,

My Route shows:

Route::get('/test', '[email protected]');

My Controller PostController looks like this:

class PostController extends Controller
    public function index()
        $posts = Post::all();

        return view('posts.index', compact('posts'));

My Model Post.php looks like this:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
    protected $connection = 'wordpress';
    protected $table = 'wp_posts';

My view looks like this:

    @foreach ($post as $entry);
            <h1>{{ $entry->post_title }}</h1>

Yet, I open the view and this is what I see:

ErrorException (E_ERROR)
Undefined variable: posts (View: /home/vagrant/code/listingnaples.test/resources/views/posts/index.blade.php)

I hate asking yet I've stared at this so long without solving it on my own. What am I doing wrong? It seems like it has to be something simple.

Please sign in or create an account to participate in this conversation.