PHPackages                             awtechs/datasource - 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. awtechs/datasource

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

awtechs/datasource
==================

A Laravel repository pattern implementation for Eloquent models with caching and query utilities

1.0.1(10mo ago)23MITPHPPHP &gt;=8.1

Since Sep 4Pushed 10mo agoCompare

[ Source](https://github.com/Lordeagle4/datasource)[ Packagist](https://packagist.org/packages/awtechs/datasource)[ GitHub Sponsors](https://github.com/sponsors/awtechs)[ RSS](/packages/awtechs-datasource/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (4)Versions (3)Used By (0)

Awtechs DataSource Package
==========================

[](#awtechs-datasource-package)

A flexible and extensible Laravel repository pattern implementation for Eloquent models, providing a clean interface for data access with built-in caching and query-building utilities.

Features
--------

[](#features)

- **Repository Pattern**: Abstracts Eloquent model interactions for cleaner, testable code.
- **Model Resolution**: Automatically or explicitly resolves Eloquent models based on repository class names or configurations.
- **Fluent Query Building**: Chainable methods for `where`, `with`, `search`, and more.
- **Caching Support**: Optional caching of query results with configurable TTL, using Laravel's cache drivers.
- **CRUD Operations**: Simplified methods for creating, updating, deleting, and restoring models.
- **Soft Deletes**: Handles soft-deleted models with `restore` and `forceDelete` methods.
- **Modular Design**: Organized into traits for maintainability and reusability.
- **Comprehensive Tests**: Unit tests covering model resolution, caching, queries, and mutations.

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

[](#requirements)

- PHP &gt;= 8.1
- Laravel &gt;= 9.x
- Composer
- PHPUnit and Mockery for running tests

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

[](#installation)

1. Install the package via Composer:

    ```
    composer require awtechs/datasource
    ```
2. (Optional) Publish the configuration file:

    ```
    php artisan vendor:publish --provider="Awtechs\DataSource\DataSourceServiceProvider"
    ```

    This creates a `config/datasource.php` file for customizing settings.

Configuration
-------------

[](#configuration)

The configuration file (`config/datasource.php`) allows you to customize the package behavior:

```
return [
    // Enable/disable automatic model resolution based on repository naming conventions
    'auto_resolve_models' => env('DATASOURCE_AUTO_RESOLVE_MODELS', true),

    // Enable/disable caching of query results
    'cache_results' => env('DATASOURCE_CACHE_RESULTS', false),

    // Default cache duration in seconds
    'cache_duration' => env('DATASOURCE_CACHE_DURATION', 3600),

    // Default items per page for pagination
    'default_per_page' => env('DATASOURCE_DEFAULT_PER_PAGE', 15),
];
```

You can override these settings in your `.env` file, e.g.:

```
DATASOURCE_AUTO_RESOLVE_MODELS=true
DATASOURCE_CACHE_RESULTS=true
DATASOURCE_CACHE_DURATION=7200
DATASOURCE_DEFAULT_PER_PAGE=20

```

Usage
-----

[](#usage)

### Creating a Repository

[](#creating-a-repository)

1. Create a repository class extending `Awtechs\DataSource\Eloquent\BaseRepository`:

    ```
    namespace App\Repositories;

    use Awtechs\DataSource\Eloquent\BaseRepository;
    use App\Models\User;

    class UserRepository extends BaseRepository
    {
        public static function model(): ?string
        {
            return User::class;
        }
    }
    ```
2. Alternatively, set the `$modelClass` property directly:

    ```
    namespace App\Repositories;

    use Awtechs\DataSource\Eloquent\BaseRepository;
    use App\Models\User;

    class UserRepository extends BaseRepository
    {
        protected ?string $modelClass = User::class;
    }
    ```
3. If `auto_resolve_models` is enabled, the repository can infer the model (e.g., `UserRepository` maps to `App\Models\User`).

### Dependency Injection

[](#dependency-injection)

Inject the repository into your controllers or services using Laravel's IoC container:

```
namespace App\Http\Controllers;

use App\Repositories\UserRepository;

class UserController extends Controller
{
    protected $users;

    public function __construct(UserRepository $users)
    {
        $this->users = $users;
    }

    public function index()
    {
        $users = $this->users->all();
        return view('users.index', compact('users'));
    }
}
```

### Example Operations

[](#example-operations)

#### Retrieve All Records

[](#retrieve-all-records)

```
$users = $this->users->all(['id', 'name']);
```

#### Paginate Results

[](#paginate-results)

```
$users = $this->users->paginate(10, ['id', 'name']);
```

#### Query with Conditions

[](#query-with-conditions)

```
$users = $this->users
    ->with(['posts'])
    ->where(['role' => 'admin'])
    ->orderBy('created_at', 'desc')
    ->all();
```

#### Search Across Columns

[](#search-across-columns)

```
$users = $this->users->search(['name', 'email'], 'john')->all();
```

#### Cache Results

[](#cache-results)

```
$users = $this->users->cacheFor(3600)->all(); // Cache for 1 hour
```

#### Create a Record

[](#create-a-record)

```
$user = $this->users->create([
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'password' => bcrypt('password'),
]);
```

#### Update a Record

[](#update-a-record)

```
$this->users->update(1, ['name' => 'Jane Doe']);
```

#### Delete a Record

[](#delete-a-record)

```
$this->users->delete(1); // Soft delete if model uses SoftDeletes
$this->users->forceDelete(1); // Permanent delete
```

#### Restore a Soft-Deleted Record

[](#restore-a-soft-deleted-record)

```
$this->users->restore(1);
```

#### Transaction

[](#transaction)

```
$this->users->transaction(function ($repo) {
    $repo->create(['name' => 'Test User', 'email' => 'test@example.com']);
    $repo->update(1, ['name' => 'Updated User']);
});
```

Testing
-------

[](#testing)

The package includes comprehensive unit tests for all functionality. To run the tests:

1. Ensure PHPUnit and Mockery are installed:

    ```
    composer require --dev phpunit/phpunit mockery/mockery
    ```
2. Copy the test files from the package's `tests/Unit` directory to your project's `tests/Unit` directory.
3. Run the tests:

    ```
    vendor/bin/phpunit
    ```

The tests cover:

- Model resolution (explicit and auto-resolution)
- Caching behavior (key generation, cache enable/disable, flushing)
- Query building (with, where, search, dynamic methods)
- Retrieval operations (all, paginate, find, findOrFail, firstWhere)
- Mutation operations (create, update, delete, restore)

Contributing
------------

[](#contributing)

Contributions are welcome! Please follow these steps:

1. Fork the repository.
2. Create a feature branch (`git checkout -b feature/my-feature`).
3. Commit your changes (`git commit -am 'Add my feature'`).
4. Push to the branch (`git push origin feature/my-feature`).
5. Create a Pull Request.

Please ensure your code follows PSR-12 standards and includes tests for new functionality.

License
-------

[](#license)

This package is open-sourced under the [MIT License](LICENSE).

Support
-------

[](#support)

For issues, questions, or suggestions, please open an issue on the [GitHub repository](https://github.com/awtechs/datasource).

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance55

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

2

Last Release

302d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/18239064?v=4)[Eagle (Mike)](/maintainers/Lordeagle4)[@Lordeagle4](https://github.com/Lordeagle4)

---

Top Contributors

[![Lordeagle4](https://avatars.githubusercontent.com/u/18239064?v=4)](https://github.com/Lordeagle4 "Lordeagle4 (4 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/awtechs-datasource/health.svg)

```
[![Health](https://phpackages.com/badges/awtechs-datasource/health.svg)](https://phpackages.com/packages/awtechs-datasource)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M96](/packages/mongodb-laravel-mongodb)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M46](/packages/kirschbaum-development-eloquent-power-joins)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)[laravel-liberu/laravel-gedcom

A package that converts gedcom files to Eloquent models

782.5k1](/packages/laravel-liberu-laravel-gedcom)

PHPackages © 2026

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