mirror of
https://github.com/archtechx/virtualcolumn.git
synced 2025-12-11 01:04:03 +00:00
Eloquent Virtual Column package.
Include timestamps in `getCustomColumns()` by default, fix tests (use new #[Test] attribute) |
||
|---|---|---|
| .github/workflows | ||
| src | ||
| tests | ||
| .gitattributes | ||
| .gitignore | ||
| .php-cs-fixer.php | ||
| check | ||
| composer.json | ||
| LICENSE | ||
| phpunit.xml | ||
| phpunit.xml.bak | ||
| README.md | ||
Eloquent Virtual Column
Installation
Supports Laravel 10, 11, and 12.
composer require stancl/virtualcolumn
Usage
Use the VirtualColumn trait on your model:
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:
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:
$myModel = MyModel::create(['foo' => 'bar']);
$myModel->update(['foo' => 'baz']);