PHPackages                             drahil/stutter - 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. drahil/stutter

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

drahil/stutter
==============

A lightweight, elegant ORM for PHP with intuitive relationship management.

v1.0.0(1y ago)01MITPHPPHP &gt;=8.0

Since Apr 26Pushed 1y ago1 watchersCompare

[ Source](https://github.com/drahil/Stutter)[ Packagist](https://packagist.org/packages/drahil/stutter)[ RSS](/packages/drahil-stutter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Stutter ORM
===========

[](#stutter-orm)

A lightweight, elegant ORM for PHP with intuitive relationship management, based on Laravel's Eloquent. Stutter is designed for educational purposes, making it easy for junior developers to understand how Eloquent works by using and reading its code.

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

[](#installation)

```
composer require drahil/stutter
```

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

[](#basic-usage)

### Connection Setup

[](#connection-setup)

```
use drahil\Stutter\Core\ConnectionManager;

ConnectionManager::addConnection([
    'driver' => 'mysql',
    'host' => 'localhost',
    'database' => 'your_database',
    'username' => 'your_username',
    'password' => 'your_password',
    'charset' => 'utf8mb4'
]);
```

### Create a Model

[](#create-a-model)

```
namespace App\Models;

use drahil\Stutter\Core\Model;

class User extends Model
{
    protected static string $table = 'users';
}
```

### Basic CRUD Operations

[](#basic-crud-operations)

#### Retrieve All Records

[](#retrieve-all-records)

```
$allUsers = User::all();
```

#### Find a Record by ID

[](#find-a-record-by-id)

```
$user = User::find(1);
```

#### Find or Fail

[](#find-or-fail)

```
try {
    $user = User::findOrFail(1);
} catch (\drahil\Stutter\Exceptions\ModelNotFoundException $e) {
    // Handle not found
}
```

#### Create a Record

[](#create-a-record)

```
$user = User::create([
    'name' => 'John Doe',
    'email' => 'john@example.com'
]);
```

#### Update a Record

[](#update-a-record)

```
$user = User::find(1);
$user->update([
    'name' => 'Jane Doe'
]);
```

#### Delete a Record

[](#delete-a-record)

```
$user = User::find(1);
$user->delete();
```

### Query Builder

[](#query-builder)

#### Basic Where Clause

[](#basic-where-clause)

```
$users = User::query()->where('active', true)->get();
```

#### Select Specific Columns

[](#select-specific-columns)

```
$users = User::query()->select(['id', 'name'])->get();
```

#### Limit Results

[](#limit-results)

```
$users = User::query()->limit(10)->get();
```

#### Order Results

[](#order-results)

```
$users = User::query()->orderBy('created_at', 'desc')->get();
```

#### Get First Result

[](#get-first-result)

```
$user = User::query()->where('email', 'john@example.com')->first();
```

#### Count Results

[](#count-results)

```
$count = User::query()->count();
```

#### Check Existence

[](#check-existence)

```
$exists = User::query()->where('email', 'john@example.com')->exists();
```

#### Where In Clause

[](#where-in-clause)

```
$users = User::query()->whereIn('id', [1, 2, 3])->get();
```

Relationships
-------------

[](#relationships)

### Define Relationships

[](#define-relationships)

```
// User model
public function profile()
{
    return $this->hasOne(Profile::class);
}

public function posts()
{
    return $this->hasMany(Post::class);
}

// Profile model
public function user()
{
    return $this->belongsTo(User::class);
}

// Post model
public function user()
{
    return $this->belongsTo(User::class);
}

public function tags()
{
    return $this->belongsToMany(Tag::class, 'post_tag', 'post_id', 'tag_id');
}

// Tag model
public function posts()
{
    return $this->belongsToMany(Post::class, 'post_tag', 'tag_id', 'post_id');
}
```

### Using Relationships

[](#using-relationships)

#### One-to-One

[](#one-to-one)

```
// Get a user's profile
$profile = User::find(1)->profile()->get();

// Get a profile's user
$user = Profile::find(1)->user()->get();
```

##### Create Related Record

[](#create-related-record)

```
// Create a profile for a user
$profile = User::find(1)->profile()->create([
    'bio' => 'New bio for user'
]);
```

#### One-to-Many

[](#one-to-many)

```
// Get a user's posts
$posts = User::find(1)->posts()->get();

// Get a post's user
$user = Post::find(1)->user()->get();
```

##### Create Related Records

[](#create-related-records)

```
// Create a profile for a user
$profile = User::find(1)->profile()->create([
    'bio' => 'New bio for user'
]);
```

#### Many-to-Many

[](#many-to-many)

```
// Get a post's tags
$tags = Post::find(1)->tags()->get();

// Get a tag's posts
$posts = Tag::find(1)->posts()->get();
```

##### Attach Related Records

[](#attach-related-records)

```
// Attach tags to a post
Post::find(1)->tags()->attach([1, 2, 3]);
```

##### Detach Related Records

[](#detach-related-records)

```
// Detach tags from a post
Post::find(1)->tags()->detach([2, 3]);
```

Full Example
------------

[](#full-example)

Here's a complete example to demonstrate the library usage:

```
