How to Build a Blog with Laravel (& Send Slack Notifications)

Laravel is one of the most popular frameworks to create a variety of websites. You can create portfolio websites, admin dashboards, e-commerce websites, and more. Most importantly, you can create blogs, a process central to this Laravel blog tutorial. Typically this, however, would require setting up a database and an admin dashboard to be able to enter the posts and their content and then display them. An easier way to do this is to use a headless CMS solution like ButterCMS. With ButterCMS, you don’t need to go through the hassles of creating and configuring your database or a dashboard to create posts. ButterCMS provides all that for you and more. You can create pages, blog posts, or your own entity types with fields that you choose based on your use cases. Then, all you need to do is retrieve that content with ButterCMS’s APIs, or SDKs, and display them in Laravel. In this tutorial, you will learn how to create a blog with Laravel and ButterCMS. Then, you’ll add integration into ButterCMS’ webhooks that will send notifications directly to your Slack channel of choice whenever a new post is published. You can find the code used in this tutorial at this GitHub repository. Table of contents Why use Laravel? Laravel Blog Tutorial Prerequisites Step One: Create a ButterCMS account Step Two: Create a Laravel project Step Three: Make a posts page Step Four: Make a single post page Step Five: Integrate Slack Conclusion Why use Laravel? Laravel is a full-stack PHP framework that can be integrated with powerful front-end frameworks such as React or Vue.js. With Laravel, you’ll be creating websites and apps that are scalable and capable of growth. In particular, Laravel is a good choice when you need to create a blog. Not only can you easily integrate it with third-party tools such as a headless CMS, but you can also add a ton of other functionalities like authentication, commenting, and more. You can also integrate with popular platforms like Slack, as you’ll see in this tutorial. While following along with this tutorial, we recommend that you refer to our list of Laravel best practices to make sure your application runs smoothly.  Laravel Blog Tutorial Prerequisites Before you start, make sure you have at least version 7.3 of PHP installed. Also, you need to have Composer installed. Step One: Create a ButterCMS account Start by creating a ButterCMS account if you don’t have one already. You can create one with a social media account or your email. You’ll also get the first 30 days free! After you create an account, you can start creating posts. In the sidebar, click on Blog Posts. After that, click on New Post at the top right. You’ll be able to enter the post’s title, content, image, and more. Once you’re done, click on Publish at the top right. Try to add a couple of posts to see them later in your Laravel blog app. Step Two: Create a Laravel project In this section, you’ll create a Laravel project and add the dependencies needed for this tutorial. Open a terminal and run the following command: composer create-project laravel/laravel butter-blog This will create a new Laravel project in a new directory called butter-blog. Change to the newly created directory: cd butter-blog Next, you’ll install ButterCMS’s PHP API client: composer require buttercms/buttercms-php In order to connect Laravel with ButterCMS using their APIs, you need to grab your account’s API token. You can find it on your dashboard under the “API Token” heading. After copying your API token, open .env and add the following new key: BUTTER_API_KEY= Make sure to set the value to your API token. You’re now all set to start showing your posts in Laravel! Step Three: Make a posts page You’ll now create a controller that will grab all posts from ButterCMS and display them in cards on the home page. To create a controller, run the following command: php artisan make:controller BlogController This will create the file app/Http/Controllers/BlogController.php. Open the file, and add the following new method: public function home() { $butterClient = new ButterCMS(env('BUTTER_API_KEY')); $postsResponse = $butterClient->fetchPosts(); return view('home', ['posts' => $postsResponse->getPosts()]); } First, you create a new client for ButterCMS using the SDK you installed earlier. Notice that you pass to the constructor the API token. You retrieve the API token from .env using the env helper function. Then, you can retrieve posts with fetchPosts on the client. This method returns a PostsResponse instance. You can then use that instance to retrieve the posts with the method getPosts. You pass the posts to the view home which you’ll create next. Create the view home in resources/views/home.blade.php with the follow

Apr 21, 2025 - 20:22
 0
How to Build a Blog with Laravel (& Send Slack Notifications)

Laravel is one of the most popular frameworks to create a variety of websites. You can create portfolio websites, admin dashboards, e-commerce websites, and more. Most importantly, you can create blogs, a process central to this Laravel blog tutorial. Typically this, however, would require setting up a database and an admin dashboard to be able to enter the posts and their content and then display them.

An easier way to do this is to use a headless CMS solution like ButterCMS. With ButterCMS, you don’t need to go through the hassles of creating and configuring your database or a dashboard to create posts. ButterCMS provides all that for you and more. You can create pages, blog posts, or your own entity types with fields that you choose based on your use cases. Then, all you need to do is retrieve that content with ButterCMS’s APIs, or SDKs, and display them in Laravel.

In this tutorial, you will learn how to create a blog with Laravel and ButterCMS. Then, you’ll add integration into ButterCMS’ webhooks that will send notifications directly to your Slack channel of choice whenever a new post is published.

You can find the code used in this tutorial at this GitHub repository.

Table of contents

  • Why use Laravel?
  • Laravel Blog Tutorial Prerequisites
  • Step One: Create a ButterCMS account
  • Step Two: Create a Laravel project
  • Step Three: Make a posts page
  • Step Four: Make a single post page
  • Step Five: Integrate Slack
  • Conclusion

banner-cta-laravel-blue.webp

Why use Laravel?

Laravel is a full-stack PHP framework that can be integrated with powerful front-end frameworks such as React or Vue.js. With Laravel, you’ll be creating websites and apps that are scalable and capable of growth.

In particular, Laravel is a good choice when you need to create a blog. Not only can you easily integrate it with third-party tools such as a headless CMS, but you can also add a ton of other functionalities like authentication, commenting, and more. You can also integrate with popular platforms like Slack, as you’ll see in this tutorial. While following along with this tutorial, we recommend that you refer to our list of Laravel best practices to make sure your application runs smoothly. 

Laravel Blog Tutorial Prerequisites

Before you start, make sure you have at least version 7.3 of PHP installed. Also, you need to have Composer installed.

Step One: Create a ButterCMS account

Start by creating a ButterCMS account if you don’t have one already. You can create one with a social media account or your email. You’ll also get the first 30 days free!

After you create an account, you can start creating posts. In the sidebar, click on Blog Posts.

ButterCMS blog post interface button

After that, click on New Post at the top right. You’ll be able to enter the post’s title, content, image, and more.

Sample of a new blog post in the ButterCMS blog engine

Once you’re done, click on Publish at the top right.

Try to add a couple of posts to see them later in your Laravel blog app.

Step Two: Create a Laravel project

In this section, you’ll create a Laravel project and add the dependencies needed for this tutorial.

Open a terminal and run the following command:

composer create-project laravel/laravel butter-blog

This will create a new Laravel project in a new directory called butter-blog.

Change to the newly created directory:

cd butter-blog

Next, you’ll install ButterCMS’s PHP API client:

composer require buttercms/buttercms-php

In order to connect Laravel with ButterCMS using their APIs, you need to grab your account’s API token. You can find it on your dashboard under the “API Token” heading.

After copying your API token, open .env and add the following new key:

    BUTTER_API_KEY=

Make sure to set the value to your API token.

You’re now all set to start showing your posts in Laravel!

Step Three: Make a posts page

You’ll now create a controller that will grab all posts from ButterCMS and display them in cards on the home page.

To create a controller, run the following command:

php artisan make:controller BlogController

This will create the file app/Http/Controllers/BlogController.php. Open the file, and add the following new method:

    public function home() {
            $butterClient = new ButterCMS(env('BUTTER_API_KEY'));
            $postsResponse = $butterClient->fetchPosts();

            return view('home', ['posts' => $postsResponse->getPosts()]);
    }

First, you create a new client for ButterCMS using the SDK you installed earlier. Notice that you pass to the constructor the API token. You retrieve the API token from .env using the env helper function.

Then, you can retrieve posts with fetchPosts on the client. This method returns a PostsResponse instance. You can then use that instance to retrieve the posts with the method getPosts. You pass the posts to the view home which you’ll create next.

Create the view home in resources/views/home.blade.php with the following content:

    
     lang="en">
    
       charset="UTF-8">
       name="viewport" content="width=device-width, initial-scale=1.0">
       http-equiv="X-UA-Compatible" content="ie=edge">
      </span>Butter Blog<span class="nt">
       href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    
    
       class="navbar navbar-light bg-light">
         class="container-fluid">
           class="navbar-brand" href="/">Butter Blog
        
class="container my-4"> class="row"> @forelse ($posts as $post) class="card col-2 col-md-4 me-2 p-0"> src="{{ $post->getFeaturedImage() }}" class="card-img-top" alt="{{ $post->getFeaturedImageAlt() }}"> class="card-body"> class="card-title">{{ $post->getTitle() }} class="card-text">{{ $post->getSummary() }} href="#" class="btn btn-primary">Read More