ุงูุณูุงู
ุนูููู
๐
ูุงุฑุจ ุชููููุง ุฌู
ูุนุงู ุจุฎูุฑ ุชุนุงููุง ูุชููู
ุงูููุงุฑุฏู ุนู ู
ูุถูุน ุฌุฏูุฏ ุชู
ุงู
ุงู ู ู
ููุด ููุงู
ูุชูุฑ ุนููู ู ูู ุงู Virtual Generated Columns ๐ฅ
ูู ุงูุจุฏุงูุฉ ูุฏุง ุงู Generated Columns ูู feature ูู ุงู MySQL ู ุธูุฑุช ู
ุน ุงู v5.7 ู ุงู MySQL ูุงูุช ุฅู ุงู feature ุฏู ุจุชุนู
ู populate ูู values ูู ุงู columns ุนูู ุญุณุจ ุงูุทุฑููุฉ ุงููู ุฅูุช ู
ุญุฏุฏูุง, ูุชุนุงููุง ูุฏุง ูุดูู ุงูู
ูุถูุน ุฏุง ุจู
ุซุงู ูุทูู ุฌุฏุงู ูู ูุงุฑููู ๐
ุชุนุงููุง ุงูุฃูู ูุนู
ู create ูู Product model ุจุงู migration ู ุงู factory ุนู ุทุฑูู ุงู command ุฏุง ๐
php artisan make:model Product -mf
ุจุนุฏ ูุฏุง ูููุชุญ ุงู products migration file ู ููุถูู ููู ุงู columns ุฏู ๐
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->decimal('unit_price', 8, 2);
$table->integer('quantity');
$table->decimal('total_price', 8, 2)
->storedAs('unit_price * quantity');
$table->timestamps();
});
}
ู ุฏูููุชู ุจุนุฏ ู
ุง ุถูููุง ุงู columns ุจุชุงุนุชูุง ุชุนุงููุง ูุนู
ู migrate ูู database ุนู ุทุฑูู ุงู command ุฏุง ๐ฏ
php artisan migrate
ูู ุฌููุง ููุชุญ ุงู structure ุงูุฎุงุต ุจุงูุฌุฏูู ุฏุง ูููุงูู ุฅู ุงู total_price column ูุนูุงู STORED GENERATED, ุทูุจ ุฏุง ู
ุนูุงู ุฅููุ ๐ค
ุฒู ู
ุง ูููุช ูุจู ูุฏุง ุฅู ุงู MySQL ุจุชุนู
ู populate ูู values ุนูู ุญุณุจ ุงู expression ุงููู ุฅูุช ุนุงูุฒู ู ุนูู ุญุณุจ ุงูู
ุซุงู ุฏุง ุฃูุง ุจููู ูู MySQL ุฅู ููู
ุฉ ุงู total_price column ูุชุชุญุณุจ ุนู ุทุฑูู ุถุฑุจ ููู
ุฉ ุงู unite_price column ูู ููู
ุฉ ุงู quantity column..ุชุนุงููุง ูุฌุฑุจ ุงูู
ูุถูุน ุฏุง ุจููุณูุง ูุฏุง ๐
ุจุณ ุงูุฃูู ุชุนุงููุง ูุนุฏู ุงู ProductFactory ู ูุฎููู ุจุงูุดูู ุฏุง ๐
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => $this->faker->name,
'unit_price' => $this->faker->randomNumber(3),
'quantity' => $this->faker->randomDigit(),
];
}
ู ูู
ุงู ููุนู
ู override ูู fillable property ูู ุงู Product model ุฒู ูุฏุง ๐
class Product extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'unit_price',
'quantity'
];
}
ุชุนุงููุง ุจูุง ูุถูู records ูู ุงูุฌุฏูู ุนุดุงู ูุดูู ุงู total_price column ููุญุตู ููู ุฅูู ุนู ุทุฑูู ุงู command ุฏุง ๐ง
php artisan tinker
ุงู command ุฏุง ูููุชุญูู Psy Shell ู ู
ู ุฎูุงูู ุฃูุฏุฑ ุงูุชุจ ุงู Laravel Code ุนุดุงู ูุชููุฐ, ููุง ุจููุง ูุถูู ุงู fake records ุจุฅุณุชุฎุฏุงู
ุงู ProductFactory ุนู ุทุฑูู ุงู command ุฏุง ๐
\App\Models\Product::factory(5)->create();
ูู ุฌููุง ููุชุญ ุงู database ูููุงุญุธ ุฅู ุงู five records ุฅุถุงููุง ุชู
ุงู
ู ูู
ุงู ุงู total_price column ุญุตูู populate ุนู ุทุฑูู ุงู expression ุงููู ุฃูุง ุญุฏุฏุชู ูู ุงู migration ๐
ู ุฏูููุชู ุจุนุฏ ู
ุง ุดูููุง ุงู storedAs ู
ูุซูุฏ ุชุนุงููุง ูุดูู ุงู virtualAs ู
ูุซูุฏ ๐ค
ุชุนุงููุง ูุถูู column ุฌุฏูุฏ ุจุฅุณุชุฎุฏุงู
ุงู virtualAs ู
ูุซูุฏ ูู ุงู products table ุนู ุทุฑูู ุงู command ุฏุง ๐
php artisan make:migration adds_v_total_price_column_to_products_table
ุงู command ุงููู ูุงุช ุฏุง ููุนู
ู create ูู
ูู migration ุฌุฏูุฏ ู ู
ู ุฎูุงูู ุฃูุฏุฑ ุฃุถูู ุงู column ุจุชุงุนู ุจุงูุทุฑููุฉ ุงููู ุฌุงูุฉ ุฏู ๐ค
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('products', function (Blueprint $table) {
$table
->decimal('v_total_price', 8, 2)
->after('total_price')
->virtualAs('unit_price * quantity');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('v_total_price');
});
}
ูู ุฌููุง ูุนู
ู migrate ุจุงู command ุงููู ุฅุณุชุฎุฏู
ูุงู ูุจู ูุฏุง ู ุฌููุง ููุชุญ ุงู database ูููุงุญุธ ุฅู ุงู column ุฏุง ู
ุด ู
ูุฌูุฏ! ุจุณ ูู ูุชุญูุง ุงู sturcture ุงูุฎุงุต ุจุงูุฌุฏูู ูููุงุญุธ ุฅูู ู
ูุฌูุฏ ุจุนุฏ ุงู total_price column ๐คฏ
ู ุฏุง ูุฑุฌุน ููููู ู
ู ููุน ุงู VIRTUAL GENERATED ู ุฏุง ุนูู ุนูุณ ุงู STORED GENERATED ๐ค
ู
ุญุชุงุฌูู ุจูุง ูุถูู ูุงู
record ุฌุฏูุฏ ุนุดุงู ูุชุฃูุฏ ุฅู ุงู expression ุดุบุงู ุชู
ุงู
, ู ุนุดุงู ูุนู
ู ูุฏุง ููุณุชุฎุฏู
ุงู command ุงููู ูุงุช ุนู ุทุฑูู ุงู Psy Shell ๐ป
ูููุงุญุธ ุฅู ูุนูุงู ุงูููู
ุงูู
ูุฌูุฏุฉ ู ููู ุชู
ุงู
, ู ุญุชู ูู ุนู
ููุง ุชุนุฏูู ุนูู ููู
ุฉ ุงู unit_price ุฃู ุงู quantity ูููุงูู ุฅู ููู
ุฉ ุงู total_price ู ุงู v_total_price ุฅุชุนุฏููุง ุจุดูู ุชููุงุฆู ู ุฏู ุนุธู
ุฉ ุงู generated columns ๐
ุทุจ ูุณุฆู ููุณูุง ุณุคุงู ูุฏุง ู ูู ุฅูู ูุงุฆุฏุฉ ุงู generated columnsุ ๐ค
ุฃูุง ุดุงูู ู
ู ูุฌูู ูุธุฑู ุฅู ูู ูุงูุฏุชูู ู ูู
ุง ูุงูุขุชู ๐
1-) ุงู storedAs ู ุงู virtualAs ู
ูุซูุฏุฒ ู
ู
ูู ุฃุนู
ููู
chaining ูู
ูุซูุฏ ู
ูู
ุฉ ุฌุฏุงู ุฅุณู
ูุง index ู ุฏู ุจุชุณุงุนุฏูุง ูู ุฅู ุงู query ุชููู ุฃุณุฑุน ูู ุนู
ููุฉ ุงู retrieving ููุฏุงุชุง.
2-) ุงูู
ูุถูุน ุฏุง ู
ุฎูููุด ุฃูุชุจ ู
ูุซูุฏ ุฅุถุงููุฉ ุนุดุงู ุฃุญุณุจ ู
ู ุฎูุงููุง ุงู total_price ู ุงู v_total_price ูู ุญุงูุฉ ุงู creation ุฃู ุงู update.
ูุจูุฏุง ูููู ุฎูุตูุง ู
ูุถูุนูุง ุงูุฌู
ูู ู ุฃุชู
ูู ุชููู ุฅุณุชูุฏุช โ