Download the PHP package brandonkerr/eloquent-from-settings without Composer

On this page you can find all versions of the php package brandonkerr/eloquent-from-settings. 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 eloquent-from-settings

Eloquent From Settings

Packagist Version Tests Static Analysis codecov

This package allows you to easily build Eloquent models and their relationships, through data settings via array or JSON passed to the model's factory.

Installation

Install the package via composer:

First, ensure that your model has a factory:

Then simply add the FromSettings trait to your factory:

Configuration

By default, the FromSettings trait behaves strictly and will throw:

While this is the recommended behaviour to ensure data integrity, you have the freedom to change these settings.

If you would like to allow for a keyed relationship that does not use the FromSettings trait, you can either set the $throwsMissingTraitException property to false:

or for a customized logic, implement FromSettingsInterface and complete the getThrowsMissingTraitException() function:

If you would like to allow for unknown keys to be silently dropped, you can either set the $throwsUnknownKeyException property to false:

or for a customized logic, implement FromSettingsInterface and complete the getThrowsUnknownKeyException() function:

Usage

Simply pass the desired settings via array to the factor's fromSettingsArray function:

or via JSON to the factor's fromSettingsJson function:

The end result will be

Full Example

This example covers Authors writing Books, which have Reviews. Full details can be found in the tests directory of this package.

Models and Schemas

Refer to the Models and Migrations directories under tests/Stubs for full details.

Author

Column/Attribute Type Notes
id unsigned bigint Primary Key
name string unique constraint added for use in a custom function test

Factories

The AuthorFactory and ReviewFactory classes are completely standard (aside from using the FromSettings trait), but the BookFactory has two custom functions to showcase additional functionality of this package:

Settings Data

Below is a commented example of an array of settings, where the author is the root of the data:

Then we simply call the Author's factory:

The end result is

BelongsTo

We aren't limited to Has__ relationships, like in the example above. We can also use a Book as the data root, and create an author for it:

The end result is one Author named Bob Jones, which has one Book titled My Book.

Custom Functions

But what if we don't want to create a new author, and don't know the author's ID that we could use to set the book's author_id value? You could create a custom function on the Book's factory that can find the author by their name, and assign the author to the book. See the forAuthor() function above for details.

The end result is again one Author named Bob Jones, which has one Book titled My Book, but since the Author named Bob Jones already existed, it would not be created again, and it would not violate our unique name constraint on the Authors table.

We can also use a custom function to accept non key-value pair settings. See the perfectReviews() function above for details.

The end result is once again one Author named Bob Jones, which has one Book titled My Book. However, this time the Book has two Reviews: one with reviewer Jane Doe, and the other with reviewer John Smith, and both with a score of 100 (as set by the perfectReviews function).

License

This package is open-sourced software licensed under the MIT license.


All versions of eloquent-from-settings with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/database Version ^9.22|^10.0
illuminate/support Version ^9.22|^10.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 brandonkerr/eloquent-from-settings contains the following files

Loading the files please wait ....