PHP Classes

File: database/migrations/02_create_table_urls.php

Recommend this page to a friend!
  Packages of Amirreza Ebrahimi   URL Shortener Application   database/migrations/02_create_table_urls.php   Download  
File: database/migrations/02_create_table_urls.php
Role: Class source
Content type: text/plain
Description: Class source
Class: URL Shortener Application
Application to create and redirect short URLs
Author: By
Last change:
Date: 7 months ago
Size: 1,479 bytes
 

Contents

Class file image Download
<?php

use Illuminate\Database\Capsule\Manager as DataBase;

class
CreateTableUrls
{
   
/**
     * Run the migrations to create the 'urls' table.
     *
     * @return void
     */
   
public function up(): void
   
{
       
DataBase::schema()->create('urls', function ($table) {
           
# Define columns for the 'urls' table
           
$table->id()->autoIncrement(); # Primary key with auto-increment

            # Foreign key to reference 'users' table
           
$table->unsignedBigInteger('created_by');
           
$table->foreign('created_by') # Foreign key relation to 'users' table
               
->references('id')
                ->
on('users')
                ->
onDelete('cascade'); # Delete URLs if user is deleted

           
$table->string('url'); # Original URL

           
$table->string('shortUrl')->unique(); # Shortened URL (must be unique)

           
$table->string('qrCode')->unique(); # QR code for the URL (must be unique)

           
$table->unsignedBigInteger('views')->default(0); # View counter with default value of 0

           
$table->timestamp('created_at')->useCurrent(); # Timestamp for record creation
       
});
    }

   
/**
     * Reverse the migrations by dropping the 'urls' table.
     *
     * @return void
     */
   
public function down(): void
   
{
       
# Drop the 'urls' table if it exists
       
DataBase::schema()->dropIfExists('urls');
    }
}