deiucanta / laravel-smart

Automatic Migrations, Validation and More
48 stars 7 forks source link

Support virtual fields for mysql #26

Closed terranc closed 6 years ago

terranc commented 6 years ago

Those are available since MySQL 5.7.

deiucanta commented 6 years ago

@terranc can you please provide us a real-world scenario where you would use virtual fields?

terranc commented 6 years ago

the migration code before:


    public function up()
    {
        Schema::create('awards', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('name', 100)->nullable()->default(null);
            $table->decimal('price', 10, 2)->default('0');
            $table->decimal('service_fee', 10, 2)->default('0');
            $table->decimal('sale_price', 10, 2)->virtualAs('`price` + `service_fee`');
            $table->softDeletes();
            $table->timestamps();
        });
    }
deiucanta commented 6 years ago

@terranc Thank you! Makes sense.

You could do this using a simple Eloquent accessor/getter. This seems to be a read-only field.

At the moment, we will not support this feature for the following reasons

terranc commented 6 years ago

Not like accessor/getter. Virtual fields make it easier to query in databases and use indexes.