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 = ?

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 = ?.

Has one

Has parent / Has children

Morphed by

Morphs many

Last updated

Was this helpful?