https://github.com/patrickcurl/laravel-computed-columns
Updated Builder/Schema for easier migrations.
https://github.com/patrickcurl/laravel-computed-columns
Last synced: 9 months ago
JSON representation
Updated Builder/Schema for easier migrations.
- Host: GitHub
- URL: https://github.com/patrickcurl/laravel-computed-columns
- Owner: patrickcurl
- License: mit
- Created: 2022-10-01T07:30:10.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-09T12:58:21.000Z (about 2 years ago)
- Last Synced: 2025-01-25T14:12:02.048Z (11 months ago)
- Language: PHP
- Size: 107 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Adds the methods: md5AsComputed, jsonFieldStoredAs, manyJsonFieldsStoredAs, concatWsStoredAs
[](https://packagist.org/packages/patrickcurl/laravel-computed-columns)
[](https://github.com/patrickcurl/laravel-computed-columns/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/patrickcurl/laravel-computed-columns/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[](https://packagist.org/packages/patrickcurl/laravel-computed-columns)
This package is an extension to laravel migrations to add some more optimized virtual/storedAs fields based on common use cases (at least my own);
## Support us
## Installation
You can install the package via composer:
```bash
composer require patrickcurl/laravel-computed-columns
```
You can publish and run the migrations with:
## Usage
I'm planning on better documentation but the simplest way to jump in is look at some code.
```php
id();
$table->json('data')->nullable();
$table->computedJsonColumns( // notice the plurality of columns.
'stored' // the type, accepts: 'stored' and 'virtual'
'data', // the json column to extract data from.
[ // The fields in the data json object to convert to computed fields.
'type',
'country',
'country_code',
'state',
'postcode',
'city',
'lat',
'lng',
], // loop over these fields and run the storedAs method see example below.
true // should each field be nullable?
);
// This is the equivalent of running:
// $field = 'data->';
// $path = 'country';
// $this->string('type')->storedAs('json_unquote(json_extract('.$field.$path.'))');
// concatWsStoredAs($column, $default, $separator, ...$fields)
$table->computedConcatWsColumn(
'stored' // type
'label', // computed column name
'data->label', // json path default value if other sources come up null.
', ', // separator
'city', // ...$fields|$fields[] which fields on data to convert from:
'state', // e.g. state will give you $model->state === $model->data->state;
'country',
'postcode'
); // This will use the default if it exists, otherwise it'll create a default either from json nested keys where fields are ['data->city', 'data->state', etc....];
// This one simply creates an id, my location example basically does a lookup of data via openstreetmaps, and caches the label, and then if someone adds that to their profile as their location it'll pull it all in at that point either from the api, or the cache, or an existing location as we're only dealing with city/state not actual full addresses.
// This isn't the most secure thing, for an id that's not critical that it's secret this is fine, but I wouldn't use it to hash passwords!
$table->computedMd5Column(
'stored', // type
'uid', // column
'label' // label column will be used, could also use data->label to use the original json to map from.
);
// alternately I could've used :
$table->computedWrappedColumn(
'stored',
'MD5',
'uid',
'label'
) ;
$table->timestamps();
});
Schema::create('locateables', function (Blueprint $table) {
$table->id();
$table->foreignId('location_id')->constrained();
$table->morphs('locateable');
$table->unique(['location_id', 'locateable_id', 'locateable_type']);
});
}
};
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Patrick Curl](https://github.com/patrickcurl)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.