Image Image Image Image Image
Scroll to Top

To Top

PHP Archives - THEMWEBS

Database migrations, PHP with Laravel

On 16, Jun 2018 | No Comments | In Database, Laravel, PHP | By Manny

Database migrations are an awesome concept in software engineering that allow you to keep track of the state of your database. When I say state I mean history, you can go back in time and update and delete fields. It is also known as a schema migration.

I work with PHP using the Laravel framework. Laravel have an awesome command line tool called artisan, which you can use you create new databases, update existing fields, rename existing columns, new columns and delete fields.

Migrations allow you to make incremental changes to you database, you can go back in history and create the future by adding new fields to your database, it really is quite amazing.

Lets build a music app together. What do all music apps need? songs, great, so we need a table to store all of our songs, great.

What will our table look like? what fields do we need? Lets think about the data that we want to store for a minute. All songs have a title, an artist(s), the date it was made and maybe a remix artist.

There we have it we have just planned out out schema for our table.

  • id
  • title
  • artist
  • remixed_by
  • released

First create Laravel project

// create project 
laravel new muziks

// rename evn file
mv .env.example .env

// generat key
php artisan key:generate

// ok lets go...

// login to mysql (local but sure use docker if you like)
mysql -uroot -proot

// create database
CREATE DATABASE muziks;
SHOW DATABASES;
+-------------------------------------+
| Database                            |
+-------------------------------------+
| muziks                              |
+-------------------------------------+

// run create
php artisan make:migration create_songs_table

You should have a time stamped file with the name of the migration

2018_06_16_194842_create_songs_table.php

Next open the migration file and lets fill in our schema, the fields of the table.

In out migration file we have up() and down() method. The up method will flesh out our planned schema. So when you do a php artisan migrate Laravel will go ahead and create the given table with the given fields. The down method will delete the table.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSongsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('songs', function (Blueprint $table) {
            // Here we adding our tables' fields 
            $table->increments('id');
            $table->string('title');
            $table->string('artist');
            $table->string('remixed_by');
            $table->integer('released');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // this will delete the table when
        Schema::dropIfExists('songs');
    }
}

Next create the migrations.

// run migrations
php artisan migrate

You should have a table like this:

If you want to rollback the migration, you can do this:

// rollback migration
php artisan migrate:rollback

23

Oct
2014

No Comments

In MySQL
PHP

By Manny

MySQL DB Installation notes on Ubuntu 14.04 LTS

On 23, Oct 2014 | No Comments | In MySQL, PHP | By Manny

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

Alternatively you can run:

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

You can start the MySQL daemon with:

You can test the MySQL daemon with mysql-test-run.pl

Changing language settings in phpMyAdmin

On 11, Jan 2012 | No Comments | In PHP, Web Development | By Manny

I recently installed XAMPP 1.7.7 on my machine at work. I needed to develop WordPress themes locally.

So as one does I opened up phpMyAdmin to set up a database for the first time and shock horror horror everything was written in German.  If you’ve encountered this problem here’s what you do…

Open up the  config.inc.php file found inside of the system folders and paste in this bit of code…

/* Set language to English*/
$cfg['Lang'] = 'en-utf-8';

Location of file on my machine C:/XAMPP/phpMyAdmin/config.sample.inc

I hope somebody finds this information useful.

Tags | ,