avatarSarahDev

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

7913

Abstract

lueprint <span class="hljs-variable">table</span>) { <span class="hljs-variable">table</span>-><span class="hljs-title function_ invoke__">id</span>(); <span class="hljs-variable">table</span>-&gt;<span class="hljs-title function_ invoke__">foreignId</span>(<span class="hljs-string">'user_id'</span>)-&gt;<span class="hljs-title function_ invoke__">constrained</span>(); <span class="hljs-variable">table</span>-><span class="hljs-title function_ invoke__">foreignId</span>(<span class="hljs-string">'post_id'</span>)-><span class="hljs-title function_ invoke__">constrained</span>(); <span class="hljs-variable">table</span>-&gt;<span class="hljs-title function_ invoke__">timestamps</span>(); }); } <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">down</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-title class_">Schema</span>::<span class="hljs-title function_ invoke__">dropIfExists</span>(<span class="hljs-string">'likes'</span>); } }</pre></div><p id="89aa">For the `comments` table:</p><div id="286a"><pre><span class="hljs-comment">// database/migrations/xxxx_xx_xx_create_comments_table.php</span> <span class="hljs-keyword">use</span> <span class="hljs-title">Illuminate</span>\<span class="hljs-title">Database</span>\<span class="hljs-title">Migrations</span>\<span class="hljs-title">Migration</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">Illuminate</span>\<span class="hljs-title">Database</span>\<span class="hljs-title">Schema</span>\<span class="hljs-title">Blueprint</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">Illuminate</span>\<span class="hljs-title">Support</span>\<span class="hljs-title">Facades</span>\<span class="hljs-title">Schema</span>; <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CreateCommentsTable</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Migration</span> </span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">up</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-title class_">Schema</span>::<span class="hljs-title function_ invoke__">create</span>(<span class="hljs-string">'comments'</span>, function (Blueprint <span class="hljs-variable">table</span>) { <span class="hljs-variable">table</span>-&gt;<span class="hljs-title function_ invoke__">id</span>(); <span class="hljs-variable">table</span>-><span class="hljs-title function_ invoke__">foreignId</span>(<span class="hljs-string">'user_id'</span>)-><span class="hljs-title function_ invoke__">constrained</span>(); <span class="hljs-variable">table</span>-&gt;<span class="hljs-title function_ invoke__">foreignId</span>(<span class="hljs-string">'post_id'</span>)-&gt;<span class="hljs-title function_ invoke__">constrained</span>(); <span class="hljs-variable">table</span>-><span class="hljs-title function_ invoke__">text</span>(<span class="hljs-string">'content'</span>); <span class="hljs-variable">table</span>-&gt;<span class="hljs-title function_ invoke__">timestamps</span>(); }); } <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">down</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-title class_">Schema</span>::<span class="hljs-title function_ invoke__">dropIfExists</span>(<span class="hljs-string">'comments'</span>); } }</pre></div><p id="b000">For the `follows` table:</p><div id="2c27"><pre><span class="hljs-comment">// database/migrations/xxxx_xx_xx_create_follows_table.php</span> <span class="hljs-keyword">use</span> <span class="hljs-title">Illuminate</span>\<span class="hljs-title">Database</span>\<span class="hljs-title">Migrations</span>\<span class="hljs-title">Migration</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">Illuminate</span>\<span class="hljs-title">Database</span>\<span class="hljs-title">Schema</span>\<span class="hljs-title">Blueprint</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">Illuminate</span>\<span class="hljs-title">Support</span>\<span class="hljs-title">Facades</span>\<span class="hljs-title">Schema</span>; <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CreateFollowsTable</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Migration</span> </span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">up</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-title class_">Schema</span>::<span class="hljs-title function_ invoke__">create</span>(<span class="hljs-string">'follows'</span>, function (Blueprint <span class="hljs-variable">table</span>) { <span class="hljs-variable">table</span>-&gt;<span class="hljs-title function_ invoke__">id</span>(); <span class="hljs-variable">table</span>-><span class="hljs-title function_ invoke__">foreignId</span>(<span class="hljs-string">'follower_id'</span>)-><span class="hljs-title function_ invoke__">constrained</span>(<span class="hljs-string">'users'</span>); <span class="hljs-variable">table</span>-&gt;<span class="hljs-title function_ invoke__">foreignId</span>(<span class="hljs-string">'following_id'</span>)-&gt;<span class="hljs-title function_ invoke__">constrained</span>(<span class="hljs-string">'users'</span>); <span class="hljs-variable">table</span>-><span class="hljs-title function_ invoke__">timestamps</span>(); }); } <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">down</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-title class_">Schema</span>::<span class="hljs-title function_ invoke__">dropIfExists</span>(<span class="hljs-string">'follows'</span>); } }</pre></div><p id="27e4">Run the migrations to create the respective tables:</p><div id="19fe"><pre>php artisan migrate</pre></div><p id="339c" type="7">Step 3: Create Social Media Routes, Controllers, and Views</p><p id="55d4">Next, let’s create routes, controllers, and views to handle social media-related operations.</p><p id="1c3c">Generate a new controller named SocialMediaController:</p><div id="8af3"><pre>php artisan <span class="hljs-built_in">make</span>:controller SocialMediaController</pre></div><p id="af30">In the SocialMediaController, define methods to manage posts, likes, comments, and followers:</p><div id="6f01"><pre><span class="hljs-comment">// app/Http/Controllers/SocialMediaController.php</span> <span class="hljs-keyword">namespace</span> <span class="hljs-title class_">App</span><span class="hljs-title class_">Http</span><span class="hljs-title class_">Controllers</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">Illuminate</span><span class="hljs-title">Http</span><span class="hljs-title">Request</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">App</span><span class="hljs-title">Models</span><span class="hljs-title">Post</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">App</span><span class="hljs-title">Models</span><span class="hljs-title">Like</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">App</span><span class="hljs-title">Models</span><span class="hljs-title">Comment</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">App</span><span class="hljs-title">Models</span><span class="hljs-title">Follow</span>; <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="

Options

hljs-title">SocialMediaController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span> </span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">index</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-variable">posts</span> = <span class="hljs-title class_">Post</span>::<span class="hljs-title function_ invoke__">with</span>(<span class="hljs-string">'user'</span>)-&gt;<span class="hljs-title function_ invoke__">latest</span>()-&gt;<span class="hljs-title function_ invoke__">get</span>(); <span class="hljs-keyword">return</span> <span class="hljs-title function_ invoke__">view</span>(<span class="hljs-string">'social.index'</span>, <span class="hljs-title function_ invoke__">compact</span>(<span class="hljs-string">'posts'</span>)); } <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createPost</span>(<span class="hljs-params">Request <span class="hljs-variable">request</span></span>) </span>{ <span class="hljs-comment">// Validate the input data</span> <span class="hljs-variable">request</span>-&gt;<span class="hljs-title function_ invoke__">validate</span>([ <span class="hljs-string">'content'</span> =&gt; <span class="hljs-string">'required|max:255'</span>, ]); <span class="hljs-comment">// Create the post</span> <span class="hljs-title function_ invoke__">auth</span>()-&gt;<span class="hljs-title function_ invoke__">user</span>()-&gt;<span class="hljs-title function_ invoke__">posts</span>()-&gt;<span class="hljs-title function_ invoke__">create</span>([ <span class="hljs-string">'content'</span> =&gt; <span class="hljs-variable">request</span>-><span class="hljs-title function_ invoke__">input</span>(<span class="hljs-string">'content'</span>), ]); <span class="hljs-keyword">return</span> <span class="hljs-title function_ invoke__">redirect</span>()-><span class="hljs-title function_ invoke__">route</span>(<span class="hljs-string">'social.index'</span>)-><span class="hljs-title function_ invoke__">with</span>(<span class="hljs-string">'success'</span>, <span class="hljs-string">'Post created successfully.'</span>); } <span class="hljs-comment">// Add methods for likes, comments, and followers here.</span> }</pre></div><p id="6164" type="7">Step 4: Create Social Media Views</p><p id="f8ea">Create the views for the social media home page, user profile, and post details.</p><p id="9019">a. Social Media Home Page View (resources/views/social/index.blade.php):</p><div id="27d5"><pre><! - resources/views/social/index.<span class="hljs-property">blade</span>.<span class="hljs-property">php</span><span class="hljs-meta">@extends</span>(<span class="hljs-string">'layouts.app'</span>) <span class="hljs-meta">@section</span>(<span class="hljs-string">'content'</span>) <h1><span class="hljs-title class_">Welcome</span> to the <span class="hljs-title class_">Social</span> <span class="hljs-title class_">Media</span> <span class="hljs-title class_">Network</span></h1> <span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span> <span class="hljs-tag"><<span class="hljs-name">form</span> <span class="hljs-attr">action</span>=<span class="hljs-string">"{{ route('social.createPost') }}"</span> <span class="hljs-attr">method</span>=<span class="hljs-string">"POST"</span>></span> @csrf <span class="hljs-tag"><<span class="hljs-name">div</span>></span> <span class="hljs-tag"><<span class="hljs-name">textarea</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"content"</span> <span class="hljs-attr">rows</span>=<span class="hljs-string">"3"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"What's on your mind?"</span>></span><span class="hljs-tag"></<span class="hljs-name">textarea</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span> <span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>></span>Post<span class="hljs-tag"></<span class="hljs-name">button</span>></span> <span class="hljs-tag"></<span class="hljs-name">form</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span></span> <span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span> @foreach (posts as post) <span class="hljs-tag"><<span class="hljs-name">div</span>></span> <span class="hljs-tag"><<span class="hljs-name">p</span>></span>{{ post-&gt;user-&gt;name }}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{{ post->content }}<span class="hljs-tag"></<span class="hljs-name">p</span>></span> <span class="hljs-tag"><<span class="hljs-name">p</span>></span>{{ $post->created_at }}<span class="hljs-tag"></<span class="hljs-name">p</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span> <span class="hljs-tag"><<span class="hljs-name">hr</span>></span> @endforeach <span class="hljs-tag"></<span class="hljs-name">div</span>></span> @endsection</span></pre></div><p id="116e" type="7">Step 5: Implement Likes, Comments, and Followers Functionality</p><p id="b380">In the SocialMediaController, add methods to handle likes, comments, and followers. You’ll also need to create views for managing these features.</p><p id="0ed0" type="7">Step 6: Define Social Media Routes and Create Views</p><p id="bb66">Add the necessary routes to routes/web.php:</p><div id="c479"><pre><span class="hljs-comment">// routes/web.php</span> <span class="hljs-keyword">use</span> <span class="hljs-title">App</span><span class="hljs-title">Http</span><span class="hljs-title">Controllers</span><span class="hljs-title">SocialMediaController</span>; <span class="hljs-title class_">Route</span>::<span class="hljs-title function_ invoke__">get</span>(<span class="hljs-string">'/social'</span>, [<span class="hljs-title class_">SocialMediaController</span>::<span class="hljs-variable language_">class</span>, <span class="hljs-string">'index'</span>])-><span class="hljs-title function_ invoke__">name</span>(<span class="hljs-string">'social.index'</span>); <span class="hljs-title class_">Route</span>::<span class="hljs-title function_ invoke__">post</span>(<span class="hljs-string">'/social/create-post '</span>, [<span class="hljs-title class_">SocialMediaController</span>::<span class="hljs-variable language_">class</span>, <span class="hljs-string">'createPost'</span>])-><span class="hljs-title function_ invoke__">name</span>(<span class="hljs-string">'social.createPost'</span>); <span class="hljs-comment">// Add routes for likes, comments, and followers here.</span> </pre></div><p id="1ac3">Finally, create views for managing likes, comments, and followers as needed.</p><p id="86dd" type="7">Step 7: Start the Development Server</p><p id="91d2">You can now start the Laravel development server and visit <a href="http://127.0.0.1:8000/social">http://127.0.0.1:8000/social`</a> to explore your social media platform.</p><p id="978c">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!</p><p id="0a45">Support me with a coffee: <a href="https://www.buymeacoffee.com/sarahdev">https://www.buymeacoffee.com/sarahdev</a></p></article></body>

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-network

Step 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 -m

Open 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 SocialMediaController

In 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>
@endsection

Step 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

Laravel
Social Media
Laravel Development
PHP
Laravel 10
Recommended from ReadMedium