[英]How to create a migration in Laravel 5 that only adds a column to an existing table
[英]How to Create a virtual calculated column in Laravel Migration from another table?
要创建虚拟计算列的表。 总列只是我期望的一个例子
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('price');
$table->integer('total')->virtualAs(sum(lots.total))->defalt(0); // virtualAs Or virtualAs
});
第二桌
Schema::create('lots', function (Blueprint $table) {
$table->id();
$table->string('description');
$table->decimal('price');
$table->integer('total') ;
$table->integer('product_id'); //relationship
});
我想获得总产品栏中的手数总和
总数不应存储在 Db 中,因为它是多余的并且可以计算。
您应该创建具有彼此关系的表和模型。
然后使用->withCount()
方法统计相关产品的批次数。
例如: Product::withCount('lots')->get()
这将为您提供与每个产品相关的手数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.