Setting up a simple blog using laravel

Written by Ben Wendt

Setting up a very simple blog with laravel is quite easy.

Installing Laravel

The easiest install method is to use composer.

Setting up routing

Edit your /app/routes.php and add the following routes:

Route::get('/', function()
{
    $posts = Post::orderBy('id', 'DESC')->get();
    return View::make('blog')->with('posts', $posts);
});


Route::get('post/{id}', function($id) {
    $post = Post::find($id);
    return View::make('post')->with('post', $post);
})->where('id', '[0-9]+');

What this is saying is that the root request should use the “blog” view, using the collection of all blog posts, ordered by descending id.

It also sets up an option to load individual posts at /post/{id}, and the id is validated to be only integers.

Setting up views

In /app/views create a file called layout.blade.php which contains the following:

<!doctype html>
<html>
    <head>
        <title>@yield('title')</title>
    </head>
    <body>
        <h1>@yield('title')</h1>

        @yield('content')
    </body>
</html>

This is just a wrapper template where the title and yield values can be swapped in from other templates.

Next, in /app/views create a file called blog.blade.php, which contains the following:

@extends('layout')
@section('title')
    The Blog @stop
@section('content')
    @foreach($posts as $post) 
        <h2>
            <a href="/laravel-blog/post/{{ $post->id }}">
                {{ $post->title }}
            </a>
        </h2>
        <div class="post">{{ $post->content }}</div>
    @endforeach
@stop

And make another view called post.blade.php containing the following:

@extends('layout')
@section('title')
    {{ $post->title }} @stop
@section('content')
    {{ $post->content }}
@stop

These view set up values for title and content. In the blog view, we show all the posts, and in the post view, we only show one. This mirrors what we set up in the routes.

Setting up the database

First, configure your MySQL database in app/config/database.php. Now you have to set up a migration. The migration that you set up will create the tables. When you installed laravel, you will have had the artisan command line app set up for you. Run the following command:

php artisan migrate:make create_posts_table

This will have made a file in the /app/database/migrations folder with a name like {datetime}_create_posts_table.php. In this file, there will be a class CreatePostsTable which will have an up and a down method. Replace these methods with the following:

public function up()
    {
        Schema::create('posts', function($table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('content');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('posts');
    }

Save the file, then from your command line, run:

php artisan migrate

This will create the table in your database (as configured in app/config/database.php) as configured in the up method of your migration class.

Conclusion

You have now set up a very simple, non-user friendly, and extremely plain-looking blog. You could add posts by making inserts directly to the table, or by setting up a new route that creates a Post then calls the save() method on it.