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

Planet PHP

Guest
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



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 https://github.com/vickris/simple-blog


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:

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret


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->increments('id');
$table->integer('user_id')->index();
$table->integer('target_id')->index(); // ID of person being followed
$table->timestamps();
});
}


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:

app/User.php

[...]
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 d

Truncated by Planet PHP, read more at the original (another 709 bytes)

Читать дальше...
 
Последнее редактирование модератором:
Сверху