当前位置:网站首页>Using computed columns in laravel
Using computed columns in laravel
2022-06-22 10:33:00 【rorg】
MySQL and SQLite( from 3.31.0 version ) Support generated column definitions . Let's see how to use computed columns in our database schema , And when we should add them to our migration .

Virtual and storage columns
There are basically two types of computed columns :virtual and stored. The main difference between the two is virtual Each time a user runs a query, it calculates , But it doesn't take up any space , however , The stored data needs some space , But the insert or update is updated every time the row is retrieved . In short : fictitious “ smaller ” but “ More slowly ”, Storage “ Bigger ” But faster .
Let's take a look at some SQL, How to create a calculated column :
drop table if exists users;
create table users (
id int auto_increment primary key,
first_name varchar(50) not null,
last_name varchar(50) not null,
salary int(10) not null,
name varchar(101) as (concat(first_name, ' ', last_name)),
insurance int(10) as (salary * 0.1) stored
);As we can see , We can generate columns from other columns in the row . In some cases , It can be very convenient , Especially if we want to automate these calculations .
Calculated columns in the migration mode
Now? , Let's see how Laravel Add calculated column in migration .
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('price');
$table->integer('tax')->virtualAs('price * 0.27')->nullable();
$table->integer('discount')->storedAs('price - 100')->nullable();
}); Please note that ,Laravel 8.x Support SQLite The computed column of .The advantage here is , It's really easy to create computed columns in our migration . This also means that we can easily add these to our model , Instead of adding calculated attributes to the model itself .
summary
In some cases , Using the generated columns is a good way . If you want to use virtual or storage , It depends on you and your situation , But they all provide good functionality , And it is possible to make the code of the framework itself smaller and clearer
边栏推荐
- nodejs 中 path.join() 和 path.resolve()的区别
- Use of libevent
- Gartner表示:云数据库发展强劲,但本地数据库仍然充满活力
- Zero‐Copy API
- 学会用VisualStudio开发人员工具查看对象模型
- 【毕业季·进击的技术er】青春不散场
- Gartner said: cloud database is developing strongly, but local database is still full of vitality
- Start from the principle of MVC and knock on an MVC framework to bring you the pleasure of being a great God
- Don't be silly enough to distinguish hash, chunkhash and contenthash
- jg_使用easyexcel读取excel_20220619
猜你喜欢

2022-06-09 工作记录--yarn/npm-Error-EPERM: operation not permitted, uv_cwd

西电AI专业排名超清北,南大蝉联全国第一 | 2022软科中国大学专业排名

AttributeError: module ‘skimage. draw‘ has no attribute ‘circle‘

蓝牙、wifi、zigbee和lora、NB-lot,通话信号,网络信号4G

Pytorch实现波阻抗反演

论文精读:Generative Adversarial Imitation Learning(生成对抗模仿学习)

Basic principles of the Internet

iNFTnews | 观点:市场降温或是让NFT应用走向台前的机会

php 数据库 mysql提问

AttributeError: module ‘skimage.draw‘ has no attribute ‘circle‘
随机推荐
TCP Congestion Identification
10-2xxe漏洞原理和案例实验演示
jg_使用easyexcel读取excel_20220619
10-2xxe vulnerability principle and case experiment demonstration
QT compile the Internet of things management platform 36- communication protocol
2022-06-09 work record --yarn/npm-error-eperm: operation not permitted, UV_ cwd
Software project management 8.3 Agile project quality activities
工作中的帕累托定律:关注结果,而不是输出
thinkphp3.2.3日志包含分析
Bluetooth, WiFi, ZigBee, Lora, Nb lot, call signal, network signal 4G
从MVC原理开始手敲一个MVC框架,带你体会当大神的乐趣
一条TCP连接时占用内存空间多少?
【jmeter】shell脚本自动执行jmeter
Encryption market plummeted, Seth triggered a new round of concern
IPO配置指南
使用pytorch mask-rcnn进行目标检测/分割训练
SQL statement of final examination for College Students
被曝泄露超 1.7 亿条隐私数据,学习通回应:尚未发现明确证据
LeetCode Algorithm 剑指 Offer 24. 反转链表
Should the theme of the IDE be bright or dark? Here comes the ultimate answer!