PHPackages                             greg0/lazer-database - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. greg0/lazer-database

ActiveLibrary[Database &amp; ORM](/categories/database)

greg0/lazer-database
====================

PHP library to use JSON files like flat-file database

2.0.2(2y ago)28816.5k↓50%384MITPHPPHP ^7.1 || ^8.0

Since Apr 16Pushed 11mo ago11 watchersCompare

[ Source](https://github.com/Lazer-Database/Lazer-Database)[ Packagist](https://packagist.org/packages/greg0/lazer-database)[ Docs](https://github.com/Greg0/Lazer-Database)[ GitHub Sponsors](https://github.com/anant-svc)[ RSS](/packages/greg0-lazer-database/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (4)Versions (42)Used By (4)

Lazer - php flat file database based on JSON files
==================================================

[](#lazer---php-flat-file-database-based-on-json-files)

[![Unit](https://github.com/Lazer-Database/Lazer-Database/actions/workflows/unit.yml/badge.svg)](https://github.com/Lazer-Database/Lazer-Database/actions/workflows/unit.yml)[![Current Version](https://camo.githubusercontent.com/da71f6d51fd9011e614bca19f5bab837790fa9bc18fde87a94ac38b8747114d6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f67726567302f6c617a65722d64617461626173652e737667)](https://packagist.org/packages/greg0/lazer-database#latest)[![PHP Version](https://camo.githubusercontent.com/842c1fa3a4ec2822c429df4ba2b4f846b363e95821dd30f7045650dd80252844/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f67726567302f6c617a65722d64617461626173652f322e302e31)](https://camo.githubusercontent.com/842c1fa3a4ec2822c429df4ba2b4f846b363e95821dd30f7045650dd80252844/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f67726567302f6c617a65722d64617461626173652f322e302e31)[![Downloads](https://camo.githubusercontent.com/5738f8052cca5c1c4e334884f2278bce292763ddcea8a506b315965ce530651b/68747470733a2f2f706f7365722e707567782e6f72672f67726567302f6c617a65722d64617461626173652f642f746f74616c2e737667)](https://camo.githubusercontent.com/5738f8052cca5c1c4e334884f2278bce292763ddcea8a506b315965ce530651b/68747470733a2f2f706f7365722e707567782e6f72672f67726567302f6c617a65722d64617461626173652f642f746f74616c2e737667)

PHP Library to use JSON files like a database.
Functionality inspired by ORM's

Requirements
------------

[](#requirements)

- PHP 7.1+
- Composer

Installation
------------

[](#installation)

Easiest way to install `Lazer Database` is to use Composer. Of course you can use your own autoloader but you must configure it properly by yourself. You can find my Package on [Packagist.org](https://packagist.org/packages/greg0/lazer-database).

To add library to your dependencies, execute:

```
composer require greg0/lazer-database

```

Tests
-----

[](#tests)

Easiest way to run unit tests is to use composer script

```
composer run test

```

You can also use docker

```
docker build -t lazer-db .
docker run -it --rm lazer-db
```

Structure of table files
------------------------

[](#structure-of-table-files)

`table_name.data.json` - table file with data
`table_name.config.json` - table file with configuration

Basic Usage
-----------

[](#basic-usage)

First of all you should define constant `LAZER_DATA_PATH` containing absolute path to folder with JSON files:

```
define('LAZER_DATA_PATH', realpath(__DIR__).'/data/'); //Path to folder with tables
```

Then set up namespace:

```
use Lazer\Classes\Database as Lazer; // example
```

### Methods

[](#methods)

##### Chain methods

[](#chain-methods)

- `setField()` - set value of field (alternative to magic `__set()`)
- `limit()` - returns results between a certain number range. Should be used right before ending method `findAll()`.
- `orderBy()` - sort rows by key in order, can order by more than one field (just chain it).
- `groupBy()` - group rows by field.
- `where()` - filter records. Alias: `and_where()`.
- `orWhere()` - other type of filtering results.
- `with()` - join other tables by defined relations

##### Ending methods

[](#ending-methods)

- `getField` - get value of field (alternative to magic `__get()`)
- `issetField` - check if field is isset (alternative to magic `__isset()`)
- `addFields()` - append new fields into existing table
- `deleteFields()` - removing fields from existing table
- `set()` - get key/value pair array argument to save.
- `save()` - insert or Update data (automatically detect if it needs an insert or update).
- `insert()` - force an insert.
- `delete()` - deleting data.
- `relations()` - returns array with table relations
- `config()` - returns object with configuration.
- `fields()` - returns array with fields name.
- `schema()` - returns assoc array with fields name and fields type `field => type`.
- `lastId()` - returns last ID from table.
- `find()` - returns one row with specified ID.
- `findAll()` - returns rows.
- `asArray()` - returns data as indexed or assoc array: `['field_name' => 'field_name']`. Should be used after ending method `findAll()` or `find()`.
- `count()` - returns the number of rows. Should be used after ending method `findAll()` or `find()`.

### Create database

[](#create-database)

```
Lazer::create('table_name', array(
    'id' => 'integer',
    'nickname' => 'string',
    {field_name} => {field_type}
));
```

More information about field types and usage in PHPDoc

### Remove database

[](#remove-database)

```
Lazer::remove('table_name');
```

### Check if a database exists

[](#check-if-a-database-exists)

```
try{
    \Lazer\Classes\Helpers\Validate::table('table_name')->exists();
} catch(\Lazer\Classes\LazerException $e){
    //Database doesn't exist
}
```

### Select

[](#select)

#### Multiple select

[](#multiple-select)

```
$table = Lazer::table('table_name')->findAll();

foreach($table as $row)
{
    print_r($row);
}
```

#### Single record select

[](#single-record-select)

```
$row = Lazer::table('table_name')->find(1);

echo $row->id; // or $row->getField('id')
```

Type ID of row in `find()` method.

You also can do something like that to get first matching record:

```
$row = Lazer::table('table_name')->where('name', '=', 'John')->find();

echo $row->id;
```

### Insert

[](#insert)

```
$row = Lazer::table('table_name');

$row->nickname = 'new_user'; // or $row->setField('nickname', 'new_user')
$row->save();
```

Do not set the ID.

### Update

[](#update)

It's very smilar to `Inserting`.

```
$row = Lazer::table('table_name')->find(1); //Edit row with ID 1

$row->nickname = 'edited_user'; // or $row->setField('nickname', 'edited_user')
$row->save();
```

You can also set many field by simple key-value array

```
$row = Lazer::table('table_name')->find(1); //Edit row with ID 1

$row->set(array(
    'nickname' => 'user',
    'email' => 'user@example.com'
));
$row->save();
```

### Remove

[](#remove)

#### Single record deleting

[](#single-record-deleting)

```
Lazer::table('table_name')->find(1)->delete(); //Will remove row with ID 1

// OR

Lazer::table('table_name')->where('name', '=', 'John')->find()->delete(); //Will remove John from DB
```

#### Multiple records deleting

[](#multiple-records-deleting)

```
Lazer::table('table_name')->where('nickname', '=', 'edited_user')->delete();
```

#### Clear table

[](#clear-table)

```
Lazer::table('table_name')->delete();
```

### Relations

[](#relations)

To work with relations use class Relation

```
use Lazer\Classes\Relation; // example
```

#### Relation types

[](#relation-types)

- `belongsTo` - relation many to one
- `hasMany` - relation one to many
- `hasAndBelongsToMany` - relation many to many

#### Methods

[](#methods-1)

##### Chain methods

[](#chain-methods-1)

- `belongsTo()` - set relation belongsTo
- `hasMany()` - set relation hasMany
- `hasAndBelongsToMany()` - set relation hasAndBelongsToMany
- `localKey()` - set relation local key
- `foreignKey()` - set relation foreign key
- `with()` - allow to work on existing relation

##### Ending methods

[](#ending-methods-1)

- `setRelation()` - creating specified relation
- `removeRelation()` - remove relation
- `getRelation()` - return informations about relation
- `getJunction()` - return name of junction table in `hasAndBelongsToMany` relation

#### Create relation

[](#create-relation)

```
Relation::table('table1')->belongsTo('table2')->localKey('table2_id')->foreignKey('id')->setRelation();
Relation::table('table2')->hasMany('table1')->localKey('id')->foreignKey('table2_id')->setRelation();
Relation::table('table2')->hasAndBelongsToMany('table1')->localKey('id')->foreignKey('id')->setRelation(); // Junction table will be crete automaticly
```

#### Remove relation

[](#remove-relation)

```
Relation::table('table1')->with('table2')->removeRelation();
```

#### Get relation information

[](#get-relation-information)

You can do it by two ways. Use Standard Database class or Relation but results will be different.

```
Relation::table('table1')->with('table2')->getRelation(); // relation with specified table
Lazer::table('table1')->relations(); // all relations
Lazer::table('table1')->relations('table2'); // relation with specified table
```

Description
-----------

[](#description)

For some examples please check [Examples](docs/examples.md) and [Tutorial](docs/tutorial.md) file. More informations you can find in PHPDoc, I think it's documented very well.

E-mail:

If you like and using/want to use my repo or you have any suggestions I will be greatful for sending me few words on e-mail.

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity42

Moderate usage in the ecosystem

Community29

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 85.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~125 days

Recently: every ~346 days

Total

29

Last Release

902d ago

Major Versions

1.2.0 → 2.0.02020-07-14

PHP version history (4 changes)1.0.0PHP &gt;=5.4

1.1.1PHP &gt;=5.6

2.0.0PHP &gt;=7.0

2.0.1PHP ^7.1 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/a73e0229d14ee3bcf21d24b6f8edb54e9b7f05fc51399f3d5a45914fd78b447d?d=identicon)[Greg0](/maintainers/Greg0)

---

Top Contributors

[![Greg0](https://avatars.githubusercontent.com/u/1397756?v=4)](https://github.com/Greg0 "Greg0 (270 commits)")[![anant-svc](https://avatars.githubusercontent.com/u/5195107?v=4)](https://github.com/anant-svc "anant-svc (30 commits)")[![ddebin](https://avatars.githubusercontent.com/u/458007?v=4)](https://github.com/ddebin "ddebin (4 commits)")[![reinos](https://avatars.githubusercontent.com/u/633730?v=4)](https://github.com/reinos "reinos (3 commits)")[![DavidePastore](https://avatars.githubusercontent.com/u/1949364?v=4)](https://github.com/DavidePastore "DavidePastore (3 commits)")[![dmitry-volkoff](https://avatars.githubusercontent.com/u/6894544?v=4)](https://github.com/dmitry-volkoff "dmitry-volkoff (2 commits)")[![simon511000](https://avatars.githubusercontent.com/u/31013205?v=4)](https://github.com/simon511000 "simon511000 (1 commits)")[![erickskrauch](https://avatars.githubusercontent.com/u/4787256?v=4)](https://github.com/erickskrauch "erickskrauch (1 commits)")[![ikbelkirasan](https://avatars.githubusercontent.com/u/4943502?v=4)](https://github.com/ikbelkirasan "ikbelkirasan (1 commits)")

---

Tags

databasedbfileflatjsonphpphp82rdbmsjsondatabasefileSimpledbflatlazer

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/greg0-lazer-database/health.svg)

```
[![Health](https://phpackages.com/badges/greg0-lazer-database/health.svg)](https://phpackages.com/packages/greg0-lazer-database)
```

###  Alternatives

[alvin0/database-json-laravel

Create and manage json databases with Laravel

344.3k](/packages/alvin0-database-json-laravel)[arcanedev/laravel-settings

This package allows you to persists configs/settings for Laravel projects.

74131.4k6](/packages/arcanedev-laravel-settings)[rah/danpu

Zero-dependency MySQL dump library for easily exporting and importing databases

64401.8k10](/packages/rah-danpu)[simple-swoole/db

A db component for Simps.

216.3k3](/packages/simple-swoole-db)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
