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

Simple Eloquent Laravel Package

volosyuk/simple-eloquent

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require volosyuk/simple-eloquent
    

    Add the service provider to config/app.php under providers:

    Volosyuk\SimpleEloquent\SimpleEloquentServiceProvider::class,
    
  2. Basic Usage: Define a model extending Volosyuk\SimpleEloquent\SimpleEloquent:

    use Volosyuk\SimpleEloquent\SimpleEloquent;
    
    class User extends SimpleEloquent
    {
        protected $table = 'users';
        protected $primaryKey = 'id';
    }
    
  3. First Query:

    $user = User::find(1);
    echo $user->name; // Access attributes directly
    
  4. Key Files:

    • SimpleEloquent.php (core class)
    • SimpleEloquentServiceProvider.php (service provider)
    • SimpleEloquentBuilder.php (query builder)

Implementation Patterns

Core Workflows

  1. Attribute Access:

    $user = User::find(1);
    $user->name; // Direct access (no getters/setters)
    $user->setAttribute('email', 'new@example.com');
    
  2. Query Building:

    $users = User::where('active', 1)->orderBy('name')->get();
    // Uses standard Eloquent query builder methods
    
  3. Relationships (Limited):

    // Define relationships in model
    public function posts()
    {
        return $this->hasMany(Post::class);
    }
    // Access via:
    $user->posts; // Collection of related models
    
  4. Mass Assignment:

    $user = User::create([
        'name' => 'John',
        'email' => 'john@example.com'
    ]);
    

Integration Tips

  • Hybrid Models: Extend both SimpleEloquent and Illuminate\Database\Eloquent\Model for advanced features:
    class HybridUser extends SimpleEloquent
    {
        use \Illuminate\Database\Eloquent\Model;
    }
    
  • Custom Attributes: Override getAttributes() to add computed properties:
    public function getAttributes()
    {
        return array_merge(parent::getAttributes(), [
            'full_name' => $this->first_name . ' ' . $this
            ->last_name
        ]);
    }
    
  • Events: Use standard Eloquent events (creating, saved, etc.) via traits:
    use \Illuminate\Database\Eloquent\Concerns\HasEvents;
    

Gotchas and Tips

Pitfalls

  1. No Getters/Setters:

    • Direct attribute access bypasses traditional Eloquent mutators/accessors.
    • Workaround: Override getAttribute()/setAttribute() in your model.
  2. Relationship Limitations:

    • Lazy-loaded relationships may not trigger SimpleEloquent logic.
    • Tip: Use eager loading (with()) for consistency.
  3. Observer Conflicts:

    • Observers attached to Model may not work as expected.
    • Fix: Reattach observers explicitly in boot():
      protected static function boot()
      {
          parent::boot();
          static::observe(UserObserver::class);
      }
      
  4. Primary Key Assumptions:

    • Assumes $primaryKey = 'id' by default. Override if using custom keys.

Debugging

  • Attribute Confusion:
    • Use getDirty() or getOriginal() to inspect changes:
      dd($user->getDirty(), $user->getOriginal());
      
  • Query Logs:
    • Enable query logging in config/database.php:
      'log' => true,
      'log_query_params' => true,
      

Extension Points

  1. Custom Query Builder:

    • Extend SimpleEloquentBuilder for domain-specific methods:
      class CustomBuilder extends \Volosyuk\SimpleEloquent\SimpleEloquentBuilder
      {
          public function active()
          {
              return $this->where('active', 1);
          }
      }
      
    • Bind it in boot():
      protected static function boot()
      {
          static::addGlobalScope(new \App\Scopes\ActiveScope);
          static::setBuilder(new CustomBuilder());
      }
      
  2. Attribute Casting:

    • Override getCasts() to handle type conversion:
      protected $casts = [
          'is_admin' => 'boolean',
          'metadata' => 'array',
      ];
      
  3. Serialization:

    • Implement JsonSerializable for custom JSON output:
      public function jsonSerialize()
      {
          return array_merge($this->attributes, [
              'formatted_name' => ucfirst($this->name)
          ]);
      }
      
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.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle