Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Share Laravel Package

jorenvanhocht/laravel-share

Laravel Share is a package for generating social media share links in Laravel applications. It provides a simple API to create share URLs for Facebook, Twitter, LinkedIn, Reddit, WhatsApp, and more, with support for custom URLs, titles, and images.

View on GitHub
Deep Wiki
Context7

Getting Started

Install with Composer and let Laravel's package discovery do the rest:

composer require jorenvanhocht/laravel-share

The service provider and the Share facade alias are auto-registered on Laravel 5.5+. There is no migration, no config file you must publish, and no Artisan command to run — the package is a thin URL builder plus an HTML renderer.

First use case — a share bar on an article page:

{{-- resources/views/posts/show.blade.php --}}
{!! Share::page($post->url, $post->title)
    ->facebook()
    ->twitter()
    ->linkedin($post->excerpt)
    ->whatsapp() !!}

Note {!! !!} — the package returns an HTML string, and it will be escaped into visible markup if you use {{ }}.

Where to look first

  1. The README on GitHub — it is short and the whole API is documented there.
  2. src/Share.php — every network is one small method building a URL; reading it takes five minutes and answers most questions (what parameters each network accepts, what the default markup looks like).
  3. Your rendered output — the default markup is minimal (<a> tags with target="_blank"), which is usually the point where you decide to either style it or replace it.

Use Share::currentPage('Title') instead of Share::page(...) when the page URL is simply the current request URL. That is fine for simple cases but see the gotchas on query strings and proxies.

Implementation Patterns

Chain per render, one method per network

Each call appends a link; chaining is order-independent and each network method accepts an optional attribute array that is merged into that anchor:

{!! Share::currentPage('Hello world')
    ->facebook(['class' => 'share-btn', 'aria-label' => 'Share on Facebook'])
    ->twitter(['class' => 'share-btn', 'aria-label' => 'Share on X'])
    ->linkedin(['class' => 'share-btn'])
    ->telegram(['class' => 'share-btn']) !!}

Wrap the output with prefix/suffix

Rather than post-processing the string, wrap the whole generated block so it drops into your markup as a list:

{!! Share::page($post->url, $post->title)
    ->facebook()
    ->twitter()
    ->linkedin()
    ->prefix('<ul class="share-bar">')
    ->suffix('</ul>') !!}

Build your own markup from raw links (the pattern you will actually ship)

The default anchors rarely match a design system. Use getRawLinks() / getRawLink('facebook') to get just the URLs, then render your own Blade partial with your own icons, rel, and tracking hooks:

// app/View/Components/ShareBar.php
public function render()
{
    $links = Share::page($this->url, $this->title)
        ->facebook()
        ->twitter()
        ->linkedin()
        ->whatsapp()
        ->getRawLinks();

    return view('components.share-bar', ['links' => $links]);
}
{{-- resources/views/components/share-bar.blade.php --}}
<nav class="share-bar" aria-label="Share this article">
    @foreach ($links as $network => $url)
        <a href="{{ $url }}"
           target="_blank"
           rel="noopener noreferrer nofollow"
           data-network="{{ $network }}">
            @include("icons.{$network}")
        </a>
    @endforeach
</nav>

This keeps the package as a URL-generation library (its real value) and leaves styling, accessibility, and analytics entirely to you.

Analytics and popups

Because you control the anchor when using raw links, add click handlers or data-* attributes for your analytics stack, or open a centered popup instead of a tab:

<a href="{{ $links['twitter'] }}"
   onclick="window.open(this.href, 'share', 'width=600,height=500'); return false;">

Choose the canonical URL deliberately

currentPage() uses the request URL. For paginated, filtered, or AMP variants it is usually better to pass the canonical URL explicitly, so shares always resolve to the same page:

{!! Share::page($post->canonical_url, $post->title)->facebook()->twitter() !!}

Email and non-JS contexts

The generated links are plain URLs, so they work in emails and RSS. Just make sure everything is absolute (https://…) — social networks will not accept relative URLs, and there is no JS fallback in an email client.

Testing

Assert on URLs, not HTML, to keep tests resilient to markup changes:

$links = Share::page('https://example.com/a', 'A')->facebook()->getRawLinks();

$this->assertStringContainsString('facebook.com/sharer', $links['facebook']);
$this->assertStringContainsString(urlencode('https://example.com/a'), $links['facebook']);

Gotchas and Tips

  • Escaping. Use {!! !!} in Blade for the HTML output. When you pass your own attribute values, escape anything user-supplied yourself with e() — do not hand raw user input to the attribute array.
  • Absolute URLs only. Relative paths silently produce broken share links. Pass route()/url() output, not '/posts/1'.
  • currentPage() picks up query strings. UTM parameters and pagination get baked into the shared URL and can fragment analytics. Prefer an explicit canonical URL when it matters.
  • Proxies and APP_URL. Behind a load balancer, currentPage() can generate http:// or an internal host. Configure TrustProxies/APP_URL before blaming the package.
  • Facebook caches the scrape. Changing your OG tags will not update an already-shared link; use Facebook's Sharing Debugger to re-scrape. The package only builds the share URL — your og:* meta tags are what actually render in the post.
  • LinkedIn's summary parameter is legacy. ->linkedin($summary) still builds the URL, but LinkedIn largely ignores the summary and pulls OG data from the page instead. Don't rely on it for messaging.
  • No popup behavior, no icons, no CSS. The package emits anchors; new-tab behavior comes from target="_blank". Add rel="noopener" and icons yourself. If your design needs SVG icons, getRawLinks() is the cleaner route.
  • WhatsApp on desktop opens WhatsApp Web via wa.me; on mobile it deep-links into the app. Expect different behavior per platform when testing.
  • Double-encoding. Pass the raw page URL and title. If you pass a URL that is already percent-encoded, the network's query string will look double-encoded and may 404 on their side.
  • Order of chain calls does not matter, but the builder is stateful across a single chain — always start from a fresh Share::page(...) / Share::currentPage(...) per render rather than stashing a half-built chain in a singleton or view composer cache.
  • Laravel version drift. Last tagged release is from early 2022. The API is stable and the package is small, but verify facade alias auto-registration on the newest Laravel majors, and be aware that there is no x() method — ->twitter() still emits the twitter.com/intent/tweet endpoint, which continues to redirect to X.
  • No per-network customization beyond URL params. If you need Instagram (no web share endpoint), Mastodon instances, or a copy-link button, build those yourself and mix them into the same partial as the package-generated links.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource