In this short tutorial, we'll show you how to use the insanely helpful jorenvanhocht/laravel-share package to add social media share buttons to every page of a Laravel application.

The quality of your site's material is widely spread via social share icons, which are incredibly beneficial in spreading your information to a bigger audience.

The social sharing option increases the likelihood that your material will receive free advertising. These buttons, which are put to every page of your website, offer a powerful way to let site visitors know about the most helpful content on your website.

You better know how to incorporate the social media share button in the Laravel project if you're a Laravel developer.

We will use the jorenvanhocht/laravel-share package to make this process tutorial simple, and you can obtain this package using the Composer tool so that you won't have to create it from scratch.

The jorenvanhocht/laravel-share package is absolutely necessary for this Laravel8 social media share button example.

It is a library built on PHP that enables you to create different social share links. You can build social share links for Facebook, Twitter, Linkedin, WhatsApp, Reddit, and Telegram after adding this package into your project.

Table of Contents

  1. Download Laravel App
  2. Add Laravel Share Package
  3. Register Laravel Share
  4. Set Up New Controller
  5. Create Route
  6. Add Social Media Share Buttons in View
  7. Start Application

Download Laravel App

Create a new Laravel app to get started; use the given command; you may alter the project name if you'd like.

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

Add Laravel Share Package

To begin installing the Laravel sharing library, open the console window in your view, type the suggested command, and then press Enter.

composer require jorenvanhocht/laravel-share

Register Laravel Share

Make sure to register the ServiceProvider and the facade, respectively, in the config/app.php file to fully utilize the package.

<?php
  return [
    'providers' => [
        ...
        ...        
        Jorenvh\Share\Providers\ShareServiceProvider::class,
    ];
    'aliases' => [
        ...
        ...                
        'Share' => Jorenvh\Share\ShareFacade::class,
    ];
  ];

Return to the console window and take the time to publish the resource and package configuration files.

php artisan vendor:publish --provider="Jorenvh\Share\Providers\ShareServiceProvider"

Remember to publish the configuration file once more after updating to the most recent version of Laravel.

Set Up New Controller

Create a controller file in which the functions for adding social media share buttons and loading the view template for displaying the share buttons in the browser are defined.

php artisan make:controller SocialShareButtonsController

In the file app/Http/Controllers/SocialShareButtonsController.php, update the code.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SocialShareButtonsController extends Controller
{
    public function ShareWidget()
    {
        $shareComponent = \Share::page(
            'https://www.codesolutionstuff.com/generate-rss-feed-in-laravel/',
            'Your share text comes here',
        )
        ->facebook()
        ->twitter()
        ->linkedin()
        ->telegram()
        ->whatsapp()        
        ->reddit();
        
        return view('posts', compact('shareComponent'));
    }
    
}

Create Route

A route must eventually be added to the route/web.php file; this makes it easier to incorporate the social sharing buttons on each page.

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SocialShareButtonsController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
*/
Route::get('/social-media-share', [SocialShareButtonsController::class,'ShareWidget']);

Add Social Media Share Buttons in View

To display the social share buttons in Laravel, navigate to resources/views and create the post.blade.php file. In this file, import the Bootstrap CSS, Font Awesome Links, and $shareComponent.

The resources/views/posts.blade.php file needs updated code.

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Implement Social Share Button in Laravel</title>
        
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"/>
        <style>
            div#social-links {
                margin: 0 auto;
                max-width: 500px;
            }
            div#social-links ul li {
                display: inline-block;
            }          
            div#social-links ul li a {
                padding: 20px;
                border: 1px solid #ccc;
                margin: 1px;
                font-size: 30px;
                color: #222;
                background-color: #ccc;
            }
        </style>
    </head>
    <body>
        <div class="container mt-4">
            <h2 class="mb-5 text-center">Laravel Social Share Buttons Example</h2>
            {!! $shareComponent !!}
        </div>
    </body>
</html>

Start Application

After ensuring that each component is in its proper location, launch the Laravel development server and open the app in your browser by entering the url provided below.

php artisan serve
http://127.0.0.1:8000/social-media-share

We tried our best to explain how to add social media share buttons to each page of your Laravel application in this fast guide.

We think we did our best and provided the finest solution for including the social network share button in Laravel by enlisting the aid of a third-party PHP library (laravel-share).


Recommended Posts

View All

How to Generate a Variety of QR Codes in a Laravel 9 App


In the Laravel application, a basic QR code generator allows you to generate several sorts of QR Codes.

Laravel 9 PHP Guzzle Http Client Examples


We will provide answers in this manual. If you've been searching the internet for a Laravel Guzzle http client example, your search is over.

Laravel 9 Livewire Form Submit Tutorial


Laravel 9 Work With Livewire Form Submit Tutorial. laravel 9 livewire form submit example, create form with laravel 9 livewire, laravel 9 livewire tut...

How to Use Yajra Datatables in Laravel 9 Application


Learn how to enhance your Laravel 9 application with Yajra Datatables. This tutorial will guide you through the steps of integrating and using it.

Laravel 9 FullCalendar Ajax Tutorial with Example


We&amp;#039;ll show you how to use the Fullcalendar JavaScript event calendar plugin to add FullCalendar to your Laravel app and create and cancel eve...