Skip to content

archtechx/virtualcolumn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

75718ed · Feb 25, 2025

History

33 Commits
Feb 25, 2025
Apr 7, 2024
Apr 7, 2024
Jul 6, 2020
Jan 26, 2022
Jan 26, 2022
Jul 6, 2020
Feb 25, 2025
Jan 26, 2022
Feb 14, 2025
Nov 8, 2023
Nov 8, 2023

Repository files navigation

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