Accessing wpdb (wordpress database) from laravel controller

Solution:

The problem is that wp-load.php boots most of the WordPress framework and WordPress has a function called __(). Apparently, so does Laravel. I tried booting wpdb by itself like this:

include('/path/to/wordpress/wp-includes/wp-db.php');
$mydb = New wpdb('user', 'pass', 'dummydb', 'localhost');
$test = $mydb->get_results("SELECT * FROM {$mydb->posts} LIMIT 5");

But it will throw undefined function errors because it uses functions in the rest of the WordPress code base, which isn’t being loaded. That means you aren’t going to be able to use $wpdb and Laravel without that function name conflict.

You really don’t need $wpdb though, at least I don’t know why you would. It isn’t really much more than a fairly limited wrapper around PHP’s mysql_* functions. It is a (minor) convenience but that is all. If you have database connection information you can do about the same thing with straight PHP.

If you needed to use WP_Query I’d understand. Some of what it does would be very painful to write by hand.