PHP code example of cyberma / laravel-layer-frame

1. Go to this page and download the library: Download cyberma/laravel-layer-frame library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cyberma / laravel-layer-frame example snippets


use Cyberma\LayerFrame\Contracts\Repositories\IRepository;
use Cyberma\LayerFrame\DBStorage\Aggregates\ScalarQuery;

final class UserRepository
{
    public function __construct(
        private readonly IRepository $repository
    ) {
    }

    public function getActiveUserCount(): int
    {
        return (int)$this->repository->scalar(
            ScalarQuery::count(),
            [['status', '=', 'active']]
        );
    }
}

// inside a semantic repository service:
$count = $this->repository->scalar(ScalarQuery::count(), [['status', '=', 'active']]);

// count(distinct userId)
$uniqueUsers = $this->repository->scalar(
    ScalarQuery::count('userId', distinct: true),
    [['status', '=', 'completed']]
);

// value(email)
$email = $this->repository->scalar(ScalarQuery::value('email'), [['id', '=', 10]]);

// flat array
$ids = $this->repository->collection(
    CollectionQuery::ids(),
    [['status', '=', 'active']]
);

// key/value map: [id => name]
$userMap = $this->repository->collection(
    CollectionQuery::pluck('name', keyColumn: 'id'),
    [['status', '=', 'active']]
);

// distinct values
$statuses = $this->repository->collection(CollectionQuery::distinctColumn('status'));

// update
$affected = $this->repository->mutate(
    MutationQuery::update(['status' => 'inactive']),
    [['lastLoginAt', '<', '2025-01-01']]
);

// increment
$updated = $this->repository->mutate(
    MutationQuery::increment('stock', 5),
    [['id', '=', 10]]
);

// AI processing / exports / indexing with low memory usage
foreach ($this->repository->stream(StreamQuery::lazy(500, ['id', 'email'])) as $row) {
    // process row
}

// stable large-table scan
foreach ($this->repository->stream(StreamQuery::chunkById(1000, 'id', ['id', 'email'])) as $row) {
    // process row
}

$count = $userRepository->getCount([['status', '=', 'active']]);

$count = (int)$userRepository->scalar(
    ScalarQuery::count(),
    [['status', '=', 'active']]
);

public function getActiveUserEmailList(): array
{
    // custom ad-hoc query code
}

public function getActiveUserEmailList(): array
{
    return $this->collection(
        CollectionQuery::pluck('email'),
        [['status', '=', 'active']]
    );
}

const TABLE = 'users';
const PRIMARY_KEY = ['id'];
const ATTRIBUTES_MAP = [
    'id'         => 'id',
    'firstName'  => 'first_name',
    'lastName'   => 'last_name',
    'roles'      => 'roles_json',
];