wp-starter/database
Database layer for wp-starter projects, providing a simple foundation for configuring and working with WordPress/MySQL connections, queries, and migrations in PHP. Designed to integrate cleanly with the wp-starter ecosystem.
Installation
composer require wp-starter/database
Publish the package config (if applicable):
php artisan vendor:publish --provider="WpStarter\Database\DatabaseServiceProvider"
Configuration
Check config/wpstarter-database.php for default settings. Key entries:
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
// ... other standard Laravel DB config
],
],
First Use Case Use the facade or service container to interact with the database:
use WpStarter\Database\Facades\Database;
// Query example
$users = Database::table('users')->where('active', 1)->get();
Database Abstraction
Database) for consistent queries across projects.$posts = Database::table('posts')->where('status', 'published')->paginate(10);
Schema Management
Schema builder via the package’s helpers:
use WpStarter\Database\Schema;
Schema::create('wp_options', function (Blueprint $table) {
$table->string('option_name')->unique();
$table->text('option_value');
});
Query Building
$results = Database::table('products')
->where('price', '>', 100)
->orderBy('created_at', 'desc')
->limit(5)
->get();
WordPress-Specific Queries
wp_get_posts wrapper), use:
$posts = Database::wp()->getPosts(['post_type' => 'product', 'numberposts' => 5]);
Laravel Mixins: Extend the package’s query builder with custom methods:
Database::extend(function ($builder) {
$builder->macro('active', function () {
return $this->where('status', 'active');
});
});
Usage:
$activeUsers = Database::table('users')->active()->get();
Event Listeners: Hook into database events (e.g., eloquent.saved) via Laravel’s event system.
Namespace Collisions
DB facade, avoid conflicts by aliasing:
'aliases' => [
'DB' => WpStarter\Database\Facades\Database::class,
],
Missing WP-Specific Docs
wpdb class.Schema Differences
wp_posts) may require custom blueprints. Example:
Schema::create('wp_posts', function (Blueprint $table) {
$table->bigIncrements('ID'); // WP uses bigint for IDs
$table->string('post_title', 255);
// ... other WP-specific columns
});
Query Logging: Enable Laravel’s query logging:
DB::enableQueryLog();
$results = Database::table('users')->get();
dd(DB::getQueryLog());
Connection Issues: Verify .env and config/wpstarter-database.php match:
DB_CONNECTION=wpstarter_mysql
Custom Connections
Add a new connection in config/wpstarter-database.php:
'connections' => [
'wp_remote' => [
'driver' => 'mysql',
'host' => env('WP_REMOTE_HOST'),
// ...
],
],
Query Observers Attach observers to models for pre/post hooks:
Database::observe(User::class, function ($model) {
// Logic before/after save
});
Seeding Use Laravel’s seeder classes with the package’s facade:
public function run()
{
Database::table('users')->insert([
['name' => 'Admin', 'email' => 'admin@example.com'],
]);
}
How can I help you explore Laravel packages today?