Leveraging MySQL Generated Columns in Laravel

Dec 01, 2023

ุงู„ุณู„ุงู… ุนู„ูŠูƒู… ๐Ÿ™Œ

 

ูŠุงุฑุจ ุชูƒูˆู†ูˆุง ุฌู…ูŠุนุงู‹ ุจุฎูŠุฑ ุชุนุงู„ูˆุง ู†ุชูƒู„ู… ุงู„ู†ู‡ุงุฑุฏู‡ ุนู† ู…ูˆุถูˆุน ุฌุฏูŠุฏ ุชู…ุงู…ุงู‹ ูˆ ู…ููŠุด ูƒู„ุงู… ูƒุชูŠุฑ ุนู„ูŠู‡ ูˆ ู‡ูˆ ุงู„ 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.

 

ูˆุจูƒุฏุง ู†ูƒูˆู† ุฎู„ุตู†ุง ู…ูˆุถูˆุนู†ุง ุงู„ุฌู…ูŠู„ ูˆ ุฃุชู…ู†ูŠ ุชูƒูˆู† ุฅุณุชูุฏุช โœ”

AI Assistant

Summarize, simplify, and ask questions about this content using your preferred AI provider.

Text Tools

Generate cleaner and easier-to-read versions of this content instantly

Have a Question?

Ask anything related to this content and get a focused AI-generated answer.

0/500
Mahmoud Ramadan
Author

Mahmoud Ramadan

Mahmoud is the creator of Digging Code and a contributor to Laravel since 2020.