PHPackages                             jamesaspence/core-model - 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. jamesaspence/core-model

Abandoned → [jamesaspence/laravel-core-model](/?search=jamesaspence%2Flaravel-core-model)ArchivedLibrary

jamesaspence/core-model
=======================

A laravel core model layer, with factories and repositories.

v1.4.0(8y ago)221[1 issues](https://github.com/jamesaspence/laravel-core-model/issues)MITPHP

Since May 17Pushed 8y ago2 watchersCompare

[ Source](https://github.com/jamesaspence/laravel-core-model)[ Packagist](https://packagist.org/packages/jamesaspence/core-model)[ RSS](/packages/jamesaspence-core-model/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (6)Dependencies (3)Versions (22)Used By (0)

Laravel Core Model (Laracore)
=============================

[](#laravel-core-model-laracore)

A repository, factory, and criteria layer for Eloquent models, providing a convenient repository interface that still allows fully-featured, eloquent usage. Allows convenient testing and dependency injection without sacrificing features or versatility.

- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
    - ["Magic" Methods](#magic-methods)
    - [Relations](#relations)
    - [Dependency Injection](#dependency-injection)
    - [Model Factories](#model-factories)
    - [Inheritance](#inheritance)
- [Future Plans](#future-plans)

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

[](#requirements)

- Laravel 5.3+

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

[](#installation)

```
composer require jamesaspence/laravel-core-model

```

Usage
-----

[](#usage)

Laracore (Laravel Core Model) is very simple to use. It works exactly the same as the Eloquent models you're already used to. This means you can use all the model queries you're already used to.

```
//First we instantiate the repository
$repository = new ModelRepository();

//We pass in the reference to the model class
//so we can query it
$repository->setModel(User::class);

//Now we use the repository to find a model
$user = $repository->find(1);

//Let's save some new attributes
$repository->fill($user, [
    'name' => 'Test Testerton'
]);

//Finally, let's save!!
$repository->save($model);
```

More advanced queries are allowed as well, including custom queries. Assuming the same repository as before:

```
//Let's query based on email AND name
$user = $repository
    ->query()
    ->where('name', '=', 'Test Testerton')
    ->where('email', '=', 'test@test.test')
    ->first();
```

The repository's methods map to the model's methods, allowing, in the above example, a return of a query builder. This means we don't lose any of the features we've come to love from Eloquent.

### "Magic" Methods

[](#magic-methods)

Laracore's repositories support the calling of magic methods, such as local scope queries. For example, consider the following code:

```
$model = User::active()->get();
```

You do not need to define a custom repository with this method hardcoded.

```
$repository = new ModelRepository(User::class);
$model = $repository->active()
    ->get();
```

Instead, we can call our scope queries and other magic methods directly on the repository. The repository will delegate them on to the model class.

Our magic method handling also listens for a model instance being the first argument of a magic method called via this repository. If the first argument is an instance of a model, it will instead call the method on the model instance itself! See the below example:

```
//This
$model = new User();
$repository->doThing($model, $stuff, $things);

//Is equivalent to this
$model->doThing($stuff, $things);
```

This is meant to catch missed repository methods that we would want implemented. If this causes issues, feel free to reach out via the issues on this repository!

### Relations

[](#relations)

Laracore also allows retrieval of relations.

```
$user = $repository
    ->with(['tokens', 'profile'])
    ->find(1);

//Let's also load a relation with an existing model.
$repository->load($existingUser, 'comments');
```

`ModelRepository` classes have a `RelationRepository`set which allows even more advanced relation settings, such as `sync` and `associate`.

```
//You can also pass in the class definition into the constructor.
$profileRepository = new ModelRepository(Profile::class);

$profile = $profileRepository->newModel(['stuff' => 'things']);

//$repository is still set for User::class here
$user = $repository->find(1);

//Assuming a BelongsTo relation named profile()
//on User, let's associate it!
$repository
    ->getRelationRepository()
    ->associateRelation($user, 'profile', $profile);

//Dont forget to save!
$repository->save($user);

//Assuming comment IDs...
$commentIds = [1, 2, 3];

//Let's sync them to a comments relation!
$repository
    ->getRelationRepository()
    ->sync($user, 'comments', $commentIds);
```

All relation methods should be represented as well, allowing versatile use.

### Dependency Injection

[](#dependency-injection)

One of the best aspects of this library is the ability to dependency inject your database access, rather than using static methods.

```
// Rather than doing this... bad!!
public function badControllerMethod()
{
    $user = User::find(1);
}

//We can do this! Good!
public function goodControllerMethod(ModelRepository $repository)
{
    $repository->setModel(User::class);

    $user = $repository->find(1);
}
```

This allows easy dependency injection, which in turn makes it very easy to isolate dependencies for testing.

### Model Factories

[](#model-factories)

Want to create models without using `new Model` all over your code? `ModelFactory` is here to help!

```
$factory = new ModelFactory();

//We need to pass in a ModelRepository
//to be able to save
$factory->setRepository(new ModelRepository(User::class));

$user = $factory->make([
    'name' => 'Test Testerton'
]);
```

This will save the model with the attributes specified.

You can also use the `ModelFactory` to save `BelongsTo`relations:

```
$user = $factory->make([
    'name' => 'Test Testerton'
], [
    'profile' => $profile
]);
```

### Inheritance

[](#inheritance)

Another nice feature is the ability to extend these classes at will. You can continue to use `ModelRepository` on its own, but if you prefer, you can extend the repositories and factories yourself.

Here, we'll extend `ModelRepository` so we don't have to set the model every time. We'll also make it so default criteria are set on the repository.

```
class UserRepository extends ModelRepository
{
   /**
    * {@inheritdoc}
    */
    public function getDefaultModel()
    {
        //We just need to pass in our default model
        return User::class;
    }
}
```

Then, we can use this without setting a model! No `setModel` required!

```
public function controllerMethod(UserRepository $repository)
{
    $user = $repository->find(1);
}
```

This will perform the following query (if using MySQL):

```
SELECT * FROM `users` WHERE `name` = ? AND `id` = ?

```

with the two bound parameters of 'Test' and '1'.

Future Plans
------------

[](#future-plans)

Short term, the plan is to keep this library compatible with major versions of Laravel &gt; 5. That means testing for new versions and adding new methods that exist in newer versions.

I would love to add non-eloquent support to this repository. The plan is to add both raw query as well as Doctrine repositories, but that isn't coming quite yet.

Long-term plans are a little more unclear. After non-eloquent support, I will probably decide on my next feature to implement. If you have any ideas, I would love to hear them!

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 98.6% 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 ~28 days

Recently: every ~22 days

Total

18

Last Release

3167d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5c7179f88787149ada074dbb41efef7ae9a8559971845c2d2b4a7db039bb4a29?d=identicon)[jamesaspence](/maintainers/jamesaspence)

---

Top Contributors

[![jamesaspence](https://avatars.githubusercontent.com/u/6488043?v=4)](https://github.com/jamesaspence "jamesaspence (143 commits)")[![trentkrogers](https://avatars.githubusercontent.com/u/16307439?v=4)](https://github.com/trentkrogers "trentkrogers (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jamesaspence-core-model/health.svg)

```
[![Health](https://phpackages.com/badges/jamesaspence-core-model/health.svg)](https://phpackages.com/packages/jamesaspence-core-model)
```

###  Alternatives

[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k43.5M5.2k](/packages/larastan-larastan)[owen-it/laravel-auditing

Audit changes of your Eloquent models in Laravel

3.4k33.0M95](/packages/owen-it-laravel-auditing)[fumeapp/modeltyper

Generate TypeScript interfaces from Laravel Models

196277.9k](/packages/fumeapp-modeltyper)[casbin/laravel-authz

An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.

324339.9k4](/packages/casbin-laravel-authz)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

44643.1k1](/packages/pressbooks-pressbooks)[illuminatech/balance

Provides support for Balance accounting system based on debit and credit principle

16137.4k](/packages/illuminatech-balance)

PHPackages © 2026

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