PHPackages                             fist/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. fist/database

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

fist/database
=============

The Fistlab PHP Database Components

56PHP

Since Feb 13Pushed 9y ago1 watchersCompare

[ Source](https://github.com/fistphp/database)[ Packagist](https://packagist.org/packages/fist/database)[ RSS](/packages/fist-database/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Fistlab Database
================

[](#fistlab-database)

[![StyleCI](https://camo.githubusercontent.com/f5507018c12e9e59992c655c505fe5b1791fa9242b751c3638324b8394def11e/68747470733a2f2f7374796c6563692e696f2f7265706f732f36373333373532372f736869656c643f7374796c653d666c6174)](https://styleci.io/repos/67337527)[![Build Status](https://camo.githubusercontent.com/6691a51da0a1db27eebb03c276ca629096b2d248f38669b82229023333ec2e2a/68747470733a2f2f7472617669732d63692e6f72672f666973747068702f64617461626173652e737667)](https://travis-ci.org/fistphp/database)[![Total Downloads](https://camo.githubusercontent.com/9ae8d92e0525673846c72acdb4e54cebbada50756b541bb63066505fbf12c799/68747470733a2f2f706f7365722e707567782e6f72672f666973742f64617461626173652f642f746f74616c2e737667)](https://packagist.org/packages/fist/database)[![Latest Stable Version](https://camo.githubusercontent.com/eb59c3c437ca4871ea1472407e401e9f2d8bd4cf1395c6222d0aee4f0544db65/68747470733a2f2f706f7365722e707567782e6f72672f666973742f64617461626173652f762f737461626c652e737667)](https://packagist.org/packages/fist/database)[![Latest Unstable Version](https://camo.githubusercontent.com/8c0f3083872f9306836b7b26ae1d4c9613d20e04256efe3c75cf1328e35c2c78/68747470733a2f2f706f7365722e707567782e6f72672f666973742f64617461626173652f762f756e737461626c652e737667)](https://packagist.org/packages/fist/database)[![License](https://camo.githubusercontent.com/1b55b11103cfc3014bf1777c92f0c6d235548948fb4f1ae03bfb0855c866fe13/68747470733a2f2f706f7365722e707567782e6f72672f666973742f64617461626173652f6c6963656e73652e737667)](https://packagist.org/packages/fist/database)

The Fistlab Database component is a database toolkit, providing an expressive query builder. It currently supports MySQL and SQLite.

Languages: **php**.

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

[](#installation)

Install using Composer.

```
composer require fist/database

```

Preparing
---------

[](#preparing)

The constructor accepts an instance of [`RepositoryInterface`](https://github.com/fistphp/repository/blob/master/RepositoryInterface.php) from [`fist/repository`](https://github.com/fistphp/repository).

Example

```
$db = new \Fist\Database\Database(
    $repository = new Fist\Repository\ArrayRepository([
        'default' => [
            'connection' => 'default',
            'driver' => 'mysql',
        ],
        'connections' => [
            'default' => [
                'driver' => 'mysql',
                'hostname' => '127.0.0.1',
                'database' => 'database',
                'username' => 'root',
                'password' => '',
            ],
        ],
        'drivers' => [
            'mysql' => \Fist\Database\Connectors\MysqlConnection::class,
        ],
    ])
);

```

> I have made more setup at this [gist](https://gist.github.com/marktopper/2f783901f19da6597b935e3b432fa41d).

Usage
-----

[](#usage)

#### Running raw statements

[](#running-raw-statements)

Raw statements can be ran by using the `statement`-method.

```
$db->statement("SELECT * FROM `users` WHERE `username` = 'mark'");

```

It also takes an optional second argument with parameters to bind. Let's do the same query but by using bindings instead.

```
$db->statement("SELECT * FROM `users` WHERE `username` = ?", ['mark'])

```

#### Selecting all rows

[](#selecting-all-rows)

Select all rows from a table using the query builder is quite easy.

```
$users = $db->table('users')->get();

foreach ($users as $user) {
    echo "Hello ".$user->username;
}

```

#### Select single row

[](#select-single-row)

Often you might want to get just a single database row object, like the current logged in user.

This can be done quite easy as well.

```
$user = $db->table('users')->first();

echo "Hello ".$user->username;

```

> Note that in case of no results. `null` will be returned. To get an exception instead use the `firstOrFail`-method.

#### Select specific columns

[](#select-specific-columns)

Want to select only specific columns, like `username`, `name` and `age`.

```
$db->table('users')->select(['username', 'name', 'age'])->get();

```

You can also use aliases for the selected columns, like you want to get `name` as `fullname`.

```
$db->table('users')->select(['username', ['name' => 'fullname'], 'age'])->get();

```

#### Where clauses

[](#where-clauses)

You can use where clauses to the query builder to filter your results.

##### Basic where clauses

[](#basic-where-clauses)

By default the operator is `=` for where clauses.

```
$db->table('users')->where('username', 'mark')->first();
$db->table('users')->where('username', '=', 'mark')->first();

```

The two methods above will do exactly the same, however you can use a set of other operators.

```
$db->table('users')->where('username', '!=', 'mark')->first();
$db->table('users')->where('age', '>', 18)->first();
$db->table('users')->where('age', '=', 18)->first();
$db->table('users')->where('age', '
