1
0
Fork 0
mirror of https://github.com/archtechx/virtualcolumn.git synced 2025-12-12 05:14:05 +00:00
Eloquent Virtual Column package.
Find a file
2022-01-04 01:23:51 +01:00
.github/workflows First try at Laravel 8 support + more advanced CI 2020-09-07 13:02:17 +03:00
src Sync with original attributes 2022-01-04 01:12:11 +01:00
tests Update VirtualColumnTest.php 2022-01-04 01:23:51 +01:00
.gitattributes Initial commit 2020-07-06 14:25:02 +02:00
.gitignore First try at Laravel 8 support + more advanced CI 2020-09-07 13:02:17 +03:00
composer.json Update composer.json 2021-12-06 16:15:52 +01:00
LICENSE Initial commit 2020-07-06 14:25:02 +02:00
phpunit.xml Initial commit 2020-07-06 14:25:02 +02:00
README.md Update README.md 2020-09-08 18:49:58 +02:00

Eloquent Virtual Column

Installation

Supports Laravel 6, 7, and 8.

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']);