Download the PHP package testinstance/laravel-model-profiles without Composer
On this page you can find all versions of the php package testinstance/laravel-model-profiles. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download testinstance/laravel-model-profiles
More information about testinstance/laravel-model-profiles
Files in testinstance/laravel-model-profiles
Package laravel-model-profiles
Short Description Laravel Model Profiles - Vertical Based Model Data Expansion On Key Value
License MIT
Informations about the package laravel-model-profiles
Laravel Model Profiles
Laravel Model Profiles allows the extension of conventional normalization forms where scaling is seen to be completed via rows representing value with data types and relational keys rather than additional tables and key based relations. Note - ONLY COMPATIBLE WITH LARAVEL FRAMEWORK
Why?
Profile tables allow the expansion or reduction of data points against a model without the need to add new columns to the original model table. For example, a 400 column table of data could be compressed down to a 1 column (primaryKey id) table.
Setup
-
(version 1) Your models must live within
App/Models
- (version 2 onward) [WIP]
Set your model path and migration path within the
app/config/profiles.php
file. These tell Laravel Model Profiles where to inject new profile models & migrations via creation. Default isApp/Models
&database/migrations
Usage
Installation
composer require testinstance/laravel-model-profiles
We will use 'User.php' model as an example for this usage guide.
Make Model Profile
In order to use Laravel Model Profiles you must use the 'use HasProfile' trait on your selected model class. 'use TestInstance\LaravelModelProfiles\Traits\HasProfile;'.
Run php artisan make:profile ModelScope\ModelClassName
- example:
php artisan make:profile User\User
. This will create a profile for the User.php Model.
You will see that two new models and a migration file are created:
App/Models/User/UserProfile.php
App/Models/User/UserProfileKey.php
database/migrations/{timestamp}_user_profile.php
Run php artisan migrate
You will see that the user_profiles
table is created with:
- id
- user_id
- user_profile_key_id
- value
- created_at
- updated_at
- deleted_at
- deleted_at_unique
You will see that the user_profile_keys
table is created with:
- id
- name
- type
- created_at
- updated_at
Interactions
Retrieve by profile value
`
$users
will be a collection of users
which have the profile value of blue
for the profile_key
name of color
whereProfile
& whereProfileIn
are a direct pass-through to the native Laravel where
& whereIn
so can be used as such. (Including searching for multiple parameters as an array: whereProfile(['color' => 'blue', 'anotherProfileKey' => 'anotherProfileValue'])
).
The profileKey type will dictate which data type it relates to and will be casted as such. The available types are listed in the Laravel documentation here. Model casting in profile is an extension and will be documented after its full release.
Save profile value
To save a profile value you can simple use the following format: ``
Eagerloading
Every model with a profile will eagerload its profile. Profile attributes can be accessed the same way model attributes are.
- example:
$user->color
will returnblue
even if the attribute 'color' does not exist as a column on user.
Notes
-
[x] Mass Assignment
Updating a model with profile attributes loaded will not be affected by profile.
-
[ ] Model Casting
Casting model profile type to another model is [WIP]
- [ ] Unit Tests
Unit tests exist for profile, however model specific tests do not. These are [WIP]. These can be created manually in the current version
Dependencies
- Laravel Framework: "^8.75" (Recommended)
- PHP: "^8.0"
Security
Please notify us of any security issues or concerns via [email protected]
License
Laravel Model Profiles is open-sourced software licensed under the MIT license.