Entity

Entity presents a table in your database.

Example


namespace Vendor\Domain\Entity;

use Pckg\Database\Entity;
use Vendor\Domain\Record\User;

class Users extends Entity
{
    protected $record = User::class;
}

Usage

Relations

All relations should be defined as an Entity method. See > Relations for more examples.

Scopes

You can extract and reuse common scopes to methods.

// ...
    public function scopeForGuest()
    {
        return $this->where('active')
            ->where('published_at', date('Y-m-d H:i:s'), '<=');
    }
// ...

You can then call scopes in your controllers, services or everywhere else.

$filteredUsers = (new Users())
    ->forGuest()
    ->all();

Extensions

You can extend the Entity

Factory

Instead of creating the entity with new, you can also create an entity from the factory.

use Pckg\Database\Entity\Factory as EntityFactory;
use Vendor\Domain\Entity\Users;

$usersEntity = EntityFactory::create(Users::class);
$usersCollection = $usersEntity->all();
$usersRecord = $usersCollection->first();

Last updated

Was this helpful?