Simple steps to Integrating Social Media Share Buttons in Laravel 11.
Social media is a powerful tool to boost website traffic, offering free promotion. We will create a Social Sharing Button for each page to allow sharing content on every social media platform we activate.
Table Of Content
1 Prerequisites
1.) PHP version of >= 8.2
2.) Composer
3.) Mysql
2 Introduction
In this tutorial, we will integrate Social Media Share Integration in Laravel to create buttons for sharing content on platforms like Facebook, Twitter, WhatsApp, and Viber. To enable this, we will install the jorenvanhocht/laravel-share package, available through composer.
3 Create / Install a Laravel Project
3.1 Install Laravel Project
First, make sure your computer has a composer.
Use the following command to install new Laravel Project.
We will create a migration for the posts table using the following command:
php artisan make:migration create_posts_table
The migration file can be found at database/migrations/. Update the file to create the posts table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->text('description');
$table->string('image');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
};
Run the migration:
php artisan migrate
5 Create Model
After creating the posts table, create a Post model at app/Models/Post.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'name',
'slug',
'description',
'image'
];
}
6 Create Factory Class
We will generate dummy data using a factory class:
php artisan make:factory PostFactory --model=Post
Update the database/factories/PostFactory.php file:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SocialShareButtonsController;
Route::get('/', function () {
return view('welcome');
});
Route::get('social-share', [SocialShareButtonsController::class, 'index']);
10 Folder Structure
11 Run Laravel Server to Test the App
Use the following artisan command to Test the App.
php artisan serve
12 Conclusion
We have successfully implemented Integrating Social Media Share Buttons in Laravel 11.