Chad Krause

A small blog about my projects

Chad's URL Shortener Part 2 - Database and Project Setup

So now it's time to get to some code. However, some planning is required. How is this database going to look?

I have 3 tables in mind so far:

  • urls and their shortened code
  • clicks and user information
  • IP Address - to save on database space

For now, it looks like this:

Starting the project

Like I said in the intro, I am going to use laravel. I created a new project, now I need to make a database migration:

php artisan make:migration inital_schema

And I need to fill it in. Note that it's not exact because I'm still learning.

class InitalSchema extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('ipaddress', function (Blueprint $table) {
            $table->increments('id');
            $table->text('address');
            $table->timestamps();
        });

        Schema::create('url', function (Blueprint $table) {
            $table->increments('id');
            $table->mediumText('url');
            $table->char('short_code', 4);
            $table->foreign('ipaddressid')->references('id')->on('ipaddress');
            $table->timestamps();

            //Unique short code
            $table->unique('short_code');
        });

        Schema::create('click', function (Blueprint $table) {
            $table->increments('id');
            $table->foreign('urlid')->references('id')->on('url');
            $table->foreign('ipaddressid')->references('id')->on('ipaddress');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

So now would be a good time to create the models, however, I am going to investigate how to route things instead.






All comments