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>


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:

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

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

    {{ $post->title }} @stop
    {{ $post->content }}

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)

    public function down()

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.


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.