I’ve been wanting to move away from WordPress for this blog for some time now. While these days I’ve spent most of my time programming in Python it makes sense to create a website blog in PHP. Since whether you like it or not it is the dominant language for web hosting platforms. This will provide flexibility where hosting is concerned.
As far as frameworks are concerned I decided upon Laravel. Firstly an old friend of mine was singing its praises plus I didn’t want to do it in Zend, which is the framework I’m familiar with. Zend in my opinion is too big a framework for a blog, it’s more suited to complex enterprise based applications.
Another reason for using Laravel is that I found what looks like a good tutorial. The steps in these articles will be in part derived from the steps in the tutorial which I’m sure will digress at certain points. The aim at the end of this project is to replace this blog with the site I build.
For this project I am using my MacBook Pro however these steps should work with little or no alteration on Linux.
composer global require "laravel/installer"
Edit ~/.bash_profile
add $HOME/.composer/vendor/bin into the path.
e.g.
export PATH=$PATH:$HOME/.composer/vendor/bin
Now create your new laravel site. Note: if the composer command is an alias to composer.phar this will not work. Try instead either renaming composer.phar to composer or creating a symbolic link.
laravel new mynewblog
While not a requirement at this point I would recommend initialising your repo.
git init
git add .
git commit -am 'my new blog site'
Note: While out of scope you could also upload the to a cloud repo like github, bitbucket or any of the many free repos. Setup the Database
Edit the .env file in your document root. You will find the below entries. Adjust these to point to you own local database instance. Note: you don’t have to use MySQL.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
You can specify a different database engine in config/database.php.
Now make your migrations with the following commands.
php artisan make:migration posts
php artisan make:migration comments
You should now see some files created in the database/migrations folder. On my installation these were as follows (the files in red):
Database Migrations
In the posts file past the following code:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Posts extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// blog table
Schema::create('posts', function (Blueprint $table)
{
$table->increments('id');
$table -> integer('author_id')->unsigned()->default(0);
$table->foreign('author_id')
->references('id')->on('users')
->onDelete('cascade');
$table->string('title')->unique();
$table->text('body');
$table->string('slug')->unique();
$table->boolean('active');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// drop blog table
Schema::drop('posts');
}
}
In the comments file post the following:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Comments extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//id, on_blog, from_user, body, at_time
Schema::create('comments', function(Blueprint $table)
{
$table->increments('id');
$table -> integer('on_post') -> unsigned() -> default(0);
$table->foreign('on_post')
->references('id')->on('posts')
->onDelete('cascade');
$table -> integer('from_user') -> unsigned() -> default(0);
$table->foreign('from_user')
->references('id')->on('users')
->onDelete('cascade');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// drop comment
Schema::drop('comments');
}
}
In the create_users_table file modify the “up” function add the highlighted line. I have also added 60 to the size of the password field. This will limit passwords to a maximum of 60 characters.
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->enum('role', ['admin','author','subscriber'])->default('author');
$table->rememberToken();
$table->timestamps();
});
}
And finally run:
php artisan migrate
You should now have the database configured and ready to go. If you had any access denied errors with the last migrate you will need to setup the permissions for your database correctly. This would also be a good time to do a commit. Something like:
git commit -am 'database setup complete'
Copyright © 2020 | Ben Hutton