Building a Social Media Network in Laravel: A Step-by-Step Tutorial

In this tutorial, we’ll guide you through the process of building a social media platform using Laravel. Users will be able to post updates, follow others, like posts, and engage in discussions. We’ll cover the essential functionalities required to create a basic social media network. Let’s get started!
Prerequisites: - Basic understanding of PHP and Laravel framework. - Composer installed on your system.
Step 1: Set Up Laravel Project
Start by creating a new Laravel project using Composer:
composer create-project - prefer-dist laravel/laravel social-media-network
cd social-media-networkStep 2: Set Up Database and Models
Configure your database credentials in the `.env` file. Next, create the models and migrations for the necessary database tables:
php artisan make:model User -m
php artisan make:model Post -m
php artisan make:model Like -m
php artisan make:model Comment -m
php artisan make:model Follow -mOpen the migration files located in the `database/migrations` directory and define the schemas for the respective tables.
For the `users` table:
// database/migrations/xxxx_xx_xx_create_users_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}For the `posts` table:
// database/migrations/xxxx_xx_xx_create_posts_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->text('content');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}For the `likes` table:
// database/migrations/xxxx_xx_xx_create_likes_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLikesTable extends Migration
{
public function up()
{
Schema::create('likes', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->foreignId('post_id')->constrained();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('likes');
}
}For the `comments` table:
// database/migrations/xxxx_xx_xx_create_comments_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCommentsTable extends Migration
{
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->foreignId('post_id')->constrained();
$table->text('content');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('comments');
}
}For the `follows` table:
// database/migrations/xxxx_xx_xx_create_follows_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateFollowsTable extends Migration
{
public function up()
{
Schema::create('follows', function (Blueprint $table) {
$table->id();
$table->foreignId('follower_id')->constrained('users');
$table->foreignId('following_id')->constrained('users');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('follows');
}
}Run the migrations to create the respective tables:
php artisan migrate
Step 3: Create Social Media Routes, Controllers, and Views
Next, let’s create routes, controllers, and views to handle social media-related operations.
Generate a new controller named `SocialMediaController`:
php artisan make:controller SocialMediaControllerIn the `SocialMediaController`, define methods to manage posts, likes, comments, and followers:
// app/Http/Controllers/SocialMediaController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use App\Models\Like;
use App\Models\Comment;
use App\Models\Follow;
class SocialMediaController extends Controller
{
public function index()
{
$posts = Post::with('user')->latest()->get();
return view('social.index', compact('posts'));
}
public function createPost(Request $request)
{
// Validate the input data
$request->validate([
'content' => 'required|max:255',
]);
// Create the post
auth()->user()->posts()->create([
'content' => $request->input('content'),
]);
return redirect()->route('social.index')->with('success', 'Post created successfully.');
}
// Add methods for likes, comments, and followers here.
}Step 4: Create Social Media Views
Create the views for the social media home page, user profile, and post details.
a. Social Media Home Page View (`resources/views/social/index.blade.php`):
<! - resources/views/social/index.blade.php →
@extends('layouts.app')
@section('content')
<h1>Welcome to the Social Media Network</h1>
<div>
<form action="{{ route('social.createPost') }}" method="POST">
@csrf
<div>
<textarea name="content" rows="3" placeholder="What's on your mind?"></textarea>
</div>
<button type="submit">Post</button>
</form>
</div>
<div>
@foreach ($posts as $post)
<div>
<p>{{ $post->user->name }}</p>
<p>{{ $post->content }}</p>
<p>{{ $post->created_at }}</p>
</div>
<hr>
@endforeach
</div>
@endsectionStep 5: Implement Likes, Comments, and Followers Functionality
In the `SocialMediaController`, add methods to handle likes, comments, and followers. You’ll also need to create views for managing these features.
Step 6: Define Social Media Routes and Create Views
Add the necessary routes to `routes/web.php`:
// routes/web.php
use App\Http\Controllers\SocialMediaController;
Route::get('/social', [SocialMediaController::class, 'index'])->name('social.index');
Route::post('/social/create-post
', [SocialMediaController::class, 'createPost'])->name('social.createPost');
// Add routes for likes, comments, and followers here.
Finally, create views for managing likes, comments, and followers as needed.
Step 7: Start the Development Server
You can now start the Laravel development server and visit `http://127.0.0.1:8000/social` to explore your social media platform.
Congratulations! You have successfully built a social media network in Laravel with features like posting updates, liking posts, commenting, and following other users. You can further enhance the project by adding user authentication, user profiles, search functionality, and more. Happy coding!
Support me with a coffee: https://www.buymeacoffee.com/sarahdev
