• Home
  • blog
  • Real-Time Laravel Notifications and Follows? Sure, with Stream!

Real-Time Laravel Notifications and Follows? Sure, with Stream!

With Laravel, it’s pretty easy to create newsfeed sites, blogs, or even forums where people post content, comment, or even mark some of these posts as favorite. To spice things up, we can make the app more lively by adding notifications for actions performed by other users. In this tutorial, we’ll be relying on a service called Stream to add this functionality to our app.

Stream is an API for Building Feeds, Activity Streams, and Notification Systems logo with Laravel colors

The API can be used with many languages and frameworks. Visit the website and click on Try the API. Select PHP as the language of choice since we’ll be working with PHP. The tutorial should give you a good overview of the type of notifications we can get with Stream. There’s an official package for Laravel, which makes it even easier to integrate this service into any Laravel app.

For this tutorial, we are going to use an existing project to try out the Stream API. Just clone this repo. It’s a simple Laravel 5.4 blog where users get to perform CRUD operations on blog posts. We’ll add the ability to follow other users shortly. We will then create feed sections with different types of notifications telling us who did what and when. Our main focus in this tutorial will be on people creating new blog posts and following each other. The complete code for this tutorial can be found here.

Project Setup

It’s recommended you use Homestead Improved for quickly getting up and running with a professional development environment that contains everything you might need.

git clone

With the repo set up locally, we should then prepare a database before running any migrations. We will use MySQL for this tutorial. After setting up the database, run:

cp .env.example .env

I have the database connection section in my .env file looking like this:


Since this is not a fresh Laravel install, we will have to run composer install to install various packages and dependencies for this project:

composer install

Then run:

php artisan migrate
php artisan db:seed

The app has some seed data to generate 10 posts. If we serve our app and visit /posts, we should be greeted with ten posts.

All set! We can now sign up new users and even create blog posts. The link to create a new post is in the navbar. Let’s add the ability to follow other users. By following another user, we’ll be updated on their activities i.e. creating new posts or following other users.

Following Users

For this, we’ll start by generating a Follow model alongside a migration. Note. however, that for large scale projects, it’s recommended to create followers and following tables to make querying relations easier:

php artisan make:model Follow -m

Let’s update the up method of the newly generated migration to this:

public function up()
    Schema::create('follows', function (Blueprint $table) {
        $table->integer('target_id')->index(); // ID of person being followed

Here, we are adding a user_id column because a follow belongs to a user. Let’s now run the migrate command to create the follows table:

php artisan migrate

We are yet to define the relationship between follows and users. Open the User model file and add the relationship:


class User extends Authenticatable
    public function follows() {
        return $this->hasMany(Follow::class);

Inside app/Follow.php, let’s add the target_id to the list of mass assignable attributes. We are also going to define the relationship specifying that a follow belongs to a user:


class Follow extends Model
    protected $fillable = ['target_id'];

    public function user()
        return $this->belongsTo(User::class);

With the relationships defined, we can now add the ability to follow other users. Let’s define the routes for the follow and unfollow actions.

Continue reading %Real-Time Laravel Notifications and Follows? Sure, with Stream!%