Download the PHP package kirkbushell/eloquence without Composer

On this page you can find all versions of the php package kirkbushell/eloquence. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package eloquence

Eloquence

Version Downloads Test

Eloquence is a package to extend Laravel's base Eloquent models and functionality.

It provides a number of utilities and attributes to work with Eloquent in new and useful ways, such as camel cased attributes (such as for JSON apis and code style cohesion), data aggregation and more.

Installation

Install the package via composer:

composer require kirkbushell/eloquence

Usage

Eloquence is automatically discoverable by Laravel, and shouldn't require any further steps. For those on earlier versions of Laravel, you can add the package as per normal in your config/app.php file:

'Eloquence\EloquenceServiceProvider',

The service provider doesn't do much, other than enable the query log, if configured.

Camel case all the things!

For those of us who prefer to work with a single coding style right across our applications, using the CamelCased trait will ensure you can do exactly that. It transforms all attribute access from camelCase to snake_case in real-time, providing a unified coding style across your application. This means everything from attribute access to JSON API responses will all be camelCased. To use, simply add the CamelCased trait to your model:

use \Eloquence\Behaviours\HasCamelCasing;

Note!

Eloquence DOES NOT CHANGE how you write your schema migrations. You should still be using snake_case when setting up your columns and tables in your database schema migrations. This is a good thing - snake_case of columns names is the defacto standard within the Laravel community and is widely-used across database schemas, as well.

Behaviours

Eloquence comes with a system for setting up behaviours, which are really just small libraries that you can use with your Eloquent models. The first of these is the count cache.

Count cache

Count caching is where you cache the result of a count on a related model's record. A simple example of this is where you have posts that belong to authors. In this situation, you may want to count the number of posts an author has regularly, and perhaps even order by this count. In SQL, ordering by an aggregated value is unable to be indexed and therefore - slow. You can get around this by caching the count of the posts the author has created on the author's model record.

To get this working, you need to do two steps:

  1. Use the HasCounts trait on the child model (in this, case Post) and
  2. Configure the count cache settings by using the CountedBy attribute.

Configuring a count cache

To setup a count cache configuration, we add the HasCounts trait, and setup the CountedBy attribute:

This tells the count cache behaviour that the model has an aggregate count cache on the Author model. So, whenever a post is added, modified or deleted, the count cache behaviour will update the appropriate author's count cache for their posts. In this case, it would update post_count field on the author model.

The example above uses the following standard conventions:

It uses your own relationship to find the related record, so no other configuration is required!

Of course, if you have a different setup, or different field names, you can alter the count cache behaviour by defining the appropriate field to update:

When setting the as: value (using named parameters here from PHP 8.0 for illustrative and readability purposes), you're telling the count cache that the aggregate field on the Author model is actually called total_posts.

HasCounts is not limited to just one count cache configuration. You can define as many as you need for each BelongsTo relationship, like so:

Sum cache

Sum caching is similar to count caching, except that instead of caching a count of the related model objects, you cache a sum of a particular field on the child model's object. A simple example of this is where you have an order that has many items. Using sum caching, you can cache the sum of all the items' prices, and store that as a cached sum on the Order model.

To get this working -- just like count caching -- you need to do two steps:

  1. Add the HasSums to your child model and
  2. Add SummedBy attribute to each relationship method that requires it.

Configure the sum cache

To setup the sum cache configuration, simply do the following:

Unlike the count cache which can assume sensible defaults, the sum cache needs a bit more guidance. The example above tells the sum cache that there is an amount field on Item that needs to be summed to the total_amount field on Order.

Cache recommendations

Because the cache system works directly with other model objects and requires multiple writes to the database, it is strongly recommended that you wrap your model saves that utilise caches in a transaction. In databases like Postgres, this is automatic, but for databases like MySQL you need to make sure you're using a transactional database engine like InnoDB.

The reason for needing transactions is that if any one of your queries fail, your caches will end up out of sync. It's better for the entire operation to fail, than to have this happen. Below is an example of using a database transaction using Laravel's DB facade:

If we return to the example above with posts having authors - if this save was not wrapped in a transaction, and the post was created but for some reason the database failed immediately after, you would never see the count cache update in the parent Author model, you'll end up with erroneous data that can be quite difficult to debug.

Sluggable

Sluggable is another behaviour that allows for the easy addition of model slugs. To use, implement the Sluggable trait:

In the example above, a slug will be created based on the username field of the User model. There are two other slugs that are supported, as well:

The only difference between the two above, is that if you're using UUIDs, the slug will be generated prior to the model being saved, based on the uuid field. With ids, which are generally auto-increase strategies - the slug has to be generated after the record has been saved - which results in a secondary save call to the database.

That's it! Easy huh?

Upgrading from v10

Version 11 of Eloquence is a complete rebuild and departure from the original codebase, utilising instead PHP 8.1 attributes and moving away from traits/class extensions where possible. This means that in some projects many updates will need to be made to ensure that your use of Eloquence continues to work.

1. Class renames

2. Updates to how caches work

All your cache implementations will need to be modified following the guide above. But in short, you'll need to import and apply the provided attributes to the relationship methods on your models that require aggregated cache values.

The best part about the new architecture with Eloquence, is that you can define your relationships however you want! If you have custom where clauses or other conditions that restrict the relationship, Eloquence will respect that. This makes Eloquence now considerably more powerful and supportive of individual domain requirements than ever before.

Let's use a real case. This is the old approach, using Countable as an example:

To migrate that to v11, we would do the following:

Note the distinct lack of required configuration. The same applies to the sum behaviour - simply migrate your configuration away from the cache functions, and into the attributes above the relationships you wish to have an aggregated cache value for.

Changelog

11.0.3

11.0.2

11.0.1

11.0.0

10.0.0

9.0.0

8.0.0

4.0.1

4.0.0

3.0.0

2.0.7

2.0.6

2.0.3

2.0.2

2.0.0

1.4.0

1.3.4

1.3.3

1.3.2

1.3.1

1.3.0

1.2.0

1.1.5

1.1.4

1.1.3

1.1.2

1.1.1

1.1.0

1.0.2

1.0.1

1.0.0

License

The Laravel framework is open-sourced software licensed under the MIT license.


All versions of eloquence with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
hashids/hashids Version ^4.1||^5.0
illuminate/database Version ^10.0||^11.0
illuminate/support Version ^10.0||^11.0
hanneskod/classtools Version ^0.1.0
symfony/finder Version ^6.3||^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package kirkbushell/eloquence contains the following files

Loading the files please wait ....