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(8mo ago)23MITPHPPHP &gt;=8.1

Since Sep 4Pushed 8mo 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 1mo ago

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

31

—

LowBetter than 68% of packages

Maintenance60

Regular maintenance activity

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity45

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

256d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/769d9c1fd3d565550249efb62c16c19719ed46b89271bb5f5c3385c6192304c3?d=identicon)[Lordeagle4](/maintainers/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

[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[genealabs/laravel-pivot-events

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

1404.9M8](/packages/genealabs-laravel-pivot-events)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[aglipanci/laravel-eloquent-case

Adds CASE statement support to Laravel Query Builder.

115157.2k](/packages/aglipanci-laravel-eloquent-case)

PHPackages © 2026

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