https://github.com/archtechx/virtualcolumn
Eloquent Virtual Column package.
https://github.com/archtechx/virtualcolumn
Last synced: 7 months ago
JSON representation
Eloquent Virtual Column package.
- Host: GitHub
- URL: https://github.com/archtechx/virtualcolumn
- Owner: archtechx
- License: mit
- Created: 2020-07-06T12:22:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-02-25T13:13:18.000Z (10 months ago)
- Last Synced: 2025-05-11T06:16:09.337Z (7 months ago)
- Language: PHP
- Size: 48.8 KB
- Stars: 77
- Watchers: 4
- Forks: 13
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Eloquent Virtual Column
## Installation
Supports Laravel 10, 11, and 12.
```
composer require stancl/virtualcolumn
```
## Usage
Use the `VirtualColumn` trait on your model:
```php
use Illuminate\Database\Eloquent\Model;
use Stancl\VirtualColumn\VirtualColumn;
class MyModel extends Model
{
use VirtualColumn;
public $guarded = [];
public static function getCustomColumns(): array
{
return [
'id',
'custom1',
'custom2',
];
}
}
```
Create a migration:
```php
public function up()
{
Schema::create('my_models', function (Blueprint $table) {
$table->increments('id');
$table->string('custom1')->nullable();
$table->string('custom2')->nullable();
$table->json('data');
});
}
```
And store any data on your model:
```php
$myModel = MyModel::create(['foo' => 'bar']);
$myModel->update(['foo' => 'baz']);
```