How to Add Facebook Like & Share Post Button in Laravel 9

Have you ever seen the Facebook Like & Share Button on posts you read? Do you even wish to add that in your content pages and share it with a larger audience? If you are making a Laravel 9 application and want to Add Facebook Like & Share Post Button, this tutorial is a must read for you!

In this guide, we will explain you for how to add Facebook Like and Share Button in an article in the Laravel application using the Facebook JavaScript SDK.

Main Steps to Add Facebook Like and Share Post Button

Step 1: Install Laravel Project

Open command line interface, type the command below and press enter to start installing a new Laravel app.

composer create-project --prefer-dist laravel/laravel laravel-demo

Step 2: Add Database Details

To link your application with data, you need to add database files. Firstly, edit your .env file as shown below.


In case you are using macOS or local MAMP server, you will also need to append UNIX_SOCKET and DB_SOCKET database credentials in your .env file.


Step 3: Model and Migrations

To add features like the Like and Share button to your blogs posts, create a model and a migration file that will hold relevant data for these.

php artisan make:model Blog -m

In the table, add title and content values, add props in the database/migrations/create_blogs_table.php file.

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateBlogsTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('blogs', function (Blueprint $table) {
     * Reverse the migrations.
     * @return void
    public function down()

Similarly, in your model file, define the table props in app/Models/Blog.php file.

namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Blog extends Model
    use HasFactory;
    protected $fillable = [

You may then run the migration command as shown below that generates a new table in the database.

php artisan migrate

Step 4: Add Fake Records with Tinker

Next, you have to generate a factory file that will allow to make fake records in the Laravel app.

php artisan make:factory BlogFactory --model=Blog

Now, get into the database\factories\BlogFactory.php file, and define the faker properties that are associated with fake records.

namespace Database\Factories;
use App\Models\Blog;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class BlogFactory extends Factory
     * The name of the factory's corresponding model.
     * @var string
    protected $model = Blog::class;
     * Define the model's default state.
     * @return array
    public function definition()
        return [
            'title' => $this->faker->name,
            'url' => Str::slug($this->faker->name),
            'category' => $this->faker->name,
            'description' => $this->faker->text,

Now all set, so we will use the tinker command that will create dummy records.

php artisan tinker

Step 5: Create Controller

You will now have to create a Controller class, where you may add the function to fetch records from database and pass them to view blade file for preview.

php artisan make:controller BlogController

The command will generate a new blog controller, you may then get into the app\Http\Controllers\BlogController.php file and update it with the given code.

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Blog;

class BlogController extends Controller
    public function index()
        $blogs = Blog::all();
        return view('index', compact('blogs'));

Step 6: Add New Routes

Now we will define the routes which will interact with the controller and retrieve results from the database. Go to the routes/web.php file and add in it the code below.

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BlogController;
| Web Routes
Route::get('/', [BlogController::class, 'index']);

Step 7: Integrate Facebook Like and Share Button in Posts

Now comes the main step, where you will integrate the Facebook Like and Share button in your blog posts. You will have to create an index.php file. get post data in it and display in bootstrap cards. Most importantly, you will have to define the Facebook JavaScript SDK here, to add the share and like buttons in Laravel. Add the code below in resources/views/index.blade.php file.

<!DOCTYPE html>
    <meta charset="utf-8">
    <title>Add Facebook Like and Share Buttons in Laravel -</title>
    <link href="" rel="stylesheet">
       .container {
           max-width: 600px;
    <div class="container mt-4">
        @foreach($blogs as $blog)
            <div class="card mb-3">
                <div class="card-body">
                    <h5 class="card-title">{{$blog->title}}</h5>
                    <p class="card-text">{{$blog->content}}</p>
                    <div class="likeShareBtnmt-3">
                        <div id="fb-root"></div>
                        <script async defer crossorigin="anonymous" src="" nonce="ccaa4s"></script>

The JavaScript SDK is available at Facebook Developer dashboard. You can get the Like and Share buttons code from there for your Laravel application. You will have to do the following steps.

  • Add the URL for your webpage where you want to place the Like button
  • Customize your Like button
  • Check a preview for your button
  • Click on the Get Code, and copy paste the code to your webpage

The image below shows the code for the Like button you can get from the JavaScript SDK.

Facebook JavaScript Like Share SDK

Step 8: Start Laravel App

That’s it, the required buttons are now added to your Laravel application. You can now start the Laravel development server.

php artisan serve

Lastly, use the URL below or your localhost URL to test the app.

You may see the Like and Share button added to your posts as shown below.

Implement Facebook Like and Share Buttons in Laravel


To sum up, to implement the Facebook Like and Share button in the Laravel application. Facebook provides us with a simple JavaScript SDK. This helps to build this small feature and integrate it with your Laravel application easily.