Pckg Documentation
Pckg Documentation
  • About
  • Concept
  • Start
  • Deployment
  • Tests
  • Ecosystem
    • Docker images
    • Skeleton
  • Core packages
    • Auth
    • Cache
    • Collection
    • Concept
    • Database
      • Repository
        • Driver
      • Entity
        • Relations
        • Query
      • Record
        • Fields
        • Model
      • Events
      • Extensions
    • Framework
      • Environment
        • Console
        • Development
        • Production
        • Test
      • Application
      • Providers
      • Config
      • Router
      • Request
      • Response
      • Controller
        • Middleware
        • Afterware
      • View
      • Events
      • Exception
    • Generic
      • Dynamic
      • Generic
      • Maestro
    • Htmlbuilder
      • Elements
        • Form
        • Fields
      • Datasources
      • Validators
      • Decorators
    • Locale
    • Mail
      • Drivers
      • Template
      • Mail
    • Manager
      • Asset manager
      • SEO manager
      • Meta manager
      • Upload manager
      • Vue manager
      • Job manager
      • Locale manager
      • Page manager
    • Migrator
      • Migrations
      • Fields
    • Queue
      • Driver
      • Publisher
      • Subscriber
    • Storage
      • Driver
      • Media
    • Translator
  • More packages
    • API
    • HTTP QL
      • Read
      • Write
      • Uploads
    • Task
      • Async
    • Websocket
      • Server
      • Client
  • Frontend
    • Helpers JS
    • Helpers CSS
  • Extras
    • Parser
    • Payment
    • Tenant
Powered by GitBook
On this page
  • Has many
  • Belongs to
  • Has many and belongs to (MTM)
  • Has one
  • Has parent / Has children
  • Morphed by
  • Morphs many

Was this helpful?

  1. Core packages
  2. Database
  3. Entity

Relations

  • Has many

  • Belongs to

  • Has and belongs to many

  • Has one

  • Has parent / Has children

Has many

When using join, this will result in ... LEFT JOIN posts ON posts.user_id = users.id .... With lazy-loading, this will result in SELECT * FROM posts WHERE user_id = ?


// ...
use Vendor\Domain\Entity\Posts;
// ...

class Users extends Entity
{
    // ...
    public function posts()
    {
        return $this->hasMany(Posts::class)
            ->foreignKey('user_id');
    }
    // ...
}

Belongs to

When using join, this will result in ... LEFT JOIN groups ON groups.id = users.group_id .... With lazy-loading, this will result in SELECT * FROM groups WHERE id = ?


// ...
use Vendor\Domain\Entity\Groups;
// ...

class Users extends Entity
{
    // ...
    public function group()
    {
        return $this->belongsTo(Groups::class)
            ->foreignKey('group_id');
    }
    // ...
}

Has many and belongs to (MTM)

When using join, this will result in ... LEFT JOIN friends ON friends.user_id = users.id LEFT JOIN users AS users2 WHERE users2.id = friends.user2_id. With lazy-loading, this will result in SELECT * FROM friends WHERE user_id = ? AND SELECT * from users WHERE id = ?.


// ...
use Vendor\Domain\Entity\Friends;
// ...

class Users extends Entity
{
    // ...
    public function friends()
    {
        return $this->hasAndBelongsTo(Users::class)
            ->over(Friends::class)
            ->leftForeignKey('user_id')
            ->rightForeignKey('user2_id');
    }
    // ...
}

Has one

Has parent / Has children


class Users extends Entity
{
    // ...
    public function parent()
    {
        return $this->hasParent(Users::class)
            ->foreignKey('parent_id');
    }

    public function children()
    {
        return $this->hasChildren(Users::class)
            ->foreignKey('parent_id');
    }
    // ...
}

Morphed by

Morphs many

PreviousEntityNextQuery

Last updated 2 years ago

Was this helpful?