laravel-social-share maintained by peal
Laravel Social Share
Smart Open Graph & multi-platform social sharing engine for Laravel.
Automatically generates optimized Open Graph (OG) metadata and provides ready-to-use share links for Facebook, X (Twitter), WhatsApp, and LinkedIn.
✨ Features
- ✅ Automatic Open Graph meta generation
- ✅ Zero-configuration setup (works out of the box)
- ✅ Multi-platform sharing (Facebook, X, WhatsApp, LinkedIn)
- ✅ Smart fallback system (no broken previews)
- ✅ Clean, extensible architecture (DTO + Generators + Engines)
📦 Installation
Install via Composer:
composer require peal/laravel-social-share
Laravel will auto-discover the package.
⚙️ Publish Configuration (Optional)
php artisan vendor:publish --tag=social-share
This creates:
config/social-share.php
🚀 Basic Usage
1. Add Open Graph Meta (Blade)
Inside your main layout <head>:
@include('social-share::meta', ['share' => $share ?? null])
2. Use in Controller
use Share;
public function show(Product $product)
{
$share = Share::for($product);
return view('product.show', compact('product', 'share'));
}
3. Add Share Buttons
<a href="{{ Share::facebook(url()->current()) }}" target="_blank">Facebook</a>
<a href="{{ Share::twitter(url()->current(), $product->name) }}" target="_blank">X</a>
<a href="{{ Share::whatsapp(url()->current(), $product->name) }}" target="_blank">WhatsApp</a>
<a href="{{ Share::linkedin(url()->current()) }}" target="_blank">LinkedIn</a>
🔥 Zero Configuration Mode (Auto OG Injection)
If enabled, the package can automatically inject meta tags.
Add Middleware
In app/Http/Kernel.php:
protected $middlewareGroups = [
'web' => [
\Peal\SocialShare\Middleware\AutoShareMetaMiddleware::class,
],
];
👉 Now no Blade or controller changes required
🧠 How It Works
The package automatically:
-
Detects page context
-
Generates share data:
- title
- description
- image
- URL
-
Injects Open Graph tags
-
Provides share URLs
⚙️ Configuration
return [
'default_image' => '/default-share.png',
];
🛒 Example: Product Share
$share = Share::for($product);
Auto generates:
- Product name → title
- Description → trimmed
- Image → primary image
- URL → product page
🧩 Extending (Custom Generator)
Create your own generator:
class BlogShareGenerator implements ShareGenerator
{
public function generate($post): ShareData
{
return ShareData::make([
'title' => $post->title,
'description' => $post->excerpt,
'image' => $post->image,
'url' => route('blog.show', $post->slug),
]);
}
}
🌍 Supported Platforms
- X (Twitter)
⚠️ Important Notes
- Facebook uses Open Graph tags only
- Content preview is cached by Facebook
- Use Facebook Debugger to refresh cache
🚀 Laravel (Inertial, vue, react ect.)
use Share;
use Inertia\Inertia;
public function show(Product $product)
{
$share = Share::for($product);
return Inertia::render('Product/Show', [
'product' => $product,
'share' => $share,
]);
}
import { Head } from '@inertiajs/react'
export default function Show({ product, share }) {
return (
<>
<Head>
<title>{share.title}</title>
<meta property="og:title" content={share.title} />
<meta property="og:description" content={share.description} />
<meta property="og:image" content={share.image} />
<meta property="og:url" content={share.url} />
<meta property="og:type" content="website" />
</Head>
<h1>{product.name}</h1>
{/* Share buttons */}
<a href={`/share/facebook?url=${share.url}`}>Facebook</a>
</>
)
}
<script setup>
import { Head } from '@inertiajs/vue3'
defineProps({
product: Object,
share: Object
})
</script>
<template>
<Head>
<title>{{ share.title }}</title>
<meta property="og:title" :content="share.title" />
<meta property="og:description" :content="share.description" />
<meta property="og:image" :content="share.image" />
<meta property="og:url" :content="share.url" />
<meta property="og:type" content="website" />
</Head>
<h1>{{ product.name }}</h1>
</template>
<a href={https://www.facebook.com/sharer/sharer.php?u=${share.url}} target="_blank">
Facebook
<a href={https://twitter.com/intent/tweet?url=${share.url}&text=${share.title}}>
X
<a href={https://wa.me/?text=${share.title} ${share.url}}>
WhatsApp
<a href={https://www.linkedin.com/sharing/share-offsite/?url=${share.url}}>
LinkedIn
🧪 Testing
composer test
🤝 Contributing
Pull requests are welcome.
📄 License
MIT License