Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Wp Eloquent Laravel Package

tareq1988/wp-eloquent

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require tareq1988/wp-eloquent
    

    Register the service provider in config/app.php:

    'providers' => [
        // ...
        Tareq1988\WPEloquent\WPEloquentServiceProvider::class,
    ],
    
  2. Basic Usage Use Eloquent models like in Laravel, but with WordPress database tables:

    use Tareq1988\WPEloquent\WPModel;
    
    class Post extends WPModel
    {
        protected $table = 'posts';
        protected $primaryKey = 'ID';
    }
    
    // Fetch a post
    $post = Post::find(1);
    
  3. First Use Case Query WordPress posts with Eloquent syntax:

    $posts = Post::where('post_status', 'publish')
                ->orderBy('post_date', 'desc')
                ->take(5)
                ->get();
    

Implementation Patterns

Core Workflows

  1. Model Definitions Extend WPModel for all WordPress tables (e.g., users, terms, comments):

    class User extends WPModel {
        protected $table = 'users';
        protected $primaryKey = 'ID';
        public $timestamps = false; // WP uses `user_registered` instead
    }
    
  2. Customizing Timestamps Override getDates() to map WordPress fields:

    protected $dates = ['user_registered', 'post_date', 'post_modified'];
    
  3. Relationships Define relationships like Laravel, but use WordPress conventions:

    class Post extends WPModel {
        public function author() {
            return $this->belongsTo(User::class, 'post_author', 'ID');
        }
    }
    
  4. Querying Meta Data Use accessors for meta fields (e.g., _wp_page_template):

    public function getPageTemplateAttribute() {
        return get_post_meta($this->ID, '_wp_page_template', true);
    }
    
  5. Bulk Operations Leverage Eloquent’s update()/delete() for batch updates:

    Post::where('post_status', 'draft')
        ->update(['post_status' => 'publish']);
    

Integration Tips

  • Hybrid Apps: Use alongside Laravel’s Eloquent for mixed DBs (e.g., custom tables + WP tables).
  • WP-CLI: Run Eloquent queries in WP-CLI scripts for admin tasks:
    wp post update 1 --post_status=draft
    
  • Caching: Cache frequent queries with remember():
    $featuredPosts = Post::where('post_type', 'post')
        ->where('meta_key', '_featured', 'meta_value', '1')
        ->remember(3600)
        ->get();
    

Gotchas and Tips

Common Pitfalls

  1. Primary Key Mismatch Always set protected $primaryKey = 'ID' (WP’s default PK). Fix: Override in every model.

  2. Timestamp Conflicts WP uses post_date/user_registered instead of created_at. Fix: Disable $timestamps or customize getDates().

  3. Meta Data Quirks Meta fields (e.g., _wp_page_template) aren’t auto-loaded. Fix: Use accessors or withMeta() trait (if available).

  4. Soft Deletes WP uses post_status = 'trash' for soft deletes. Override:

    protected $softDelete = true;
    protected $deletedAt = 'post_date';
    
  5. Case Sensitivity WP tables use lowercase, but queries may fail if case-sensitive. Fix: Use snake_case in queries (e.g., post_status not PostStatus).

Debugging Tips

  • Query Logs: Enable Laravel’s query logging to inspect generated SQL:
    DB::enableQueryLog();
    Post::all();
    dd(DB::getQueryLog());
    
  • WP Debug: Combine with WP_DEBUG for deeper insights:
    define('WP_DEBUG', true);
    define('WP_DEBUG_LOG', true);
    

Extension Points

  1. Custom Query Builder Extend Tareq1988\WPEloquent\WPBuilder for table-specific logic:

    class CustomPostBuilder extends WPBuilder {
        public function published() {
            return $this->where('post_status', 'publish');
        }
    }
    
  2. Scopes Add reusable query scopes:

    class Post extends WPModel {
        public function scopePublished($query) {
            return $query->where('post_status', 'publish');
        }
    }
    // Usage: Post::published()->get();
    
  3. Events Listen to WP events (e.g., save_post) and trigger Eloquent events:

    add_action('save_post', function($postId) {
        $post = Post::find($postId);
        event('eloquent.saved: Post', $post);
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware