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/[email protected]/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

whereIn and whereNotIn Query Example in Laravel


In this model, we will see the Laravel whereIn and whereNotIn query model. Laravel whereIn and whereNotIn query example, laravel whereIn, laravel wher...

Laravel Eloquent WHERE Like Query Example Tutorial


Laravel provides a query builder that helps us to deal with such a situation in MySQL. In this tutorial, you'll learn how to use a select where like q...

Laravel Eloquent Query - Laravel Group By with Order By Desc


You&amp;#039;ve come to the right place if you&amp;#039;re looking for an example of a laravel eloquent group by order by desc.

Laravel CORS Example: How to Enable CORS in Laravel?


In this tutorial, i will teach you how to easily enable CORS (Cross-Origin Resource Sharing) in Laravel and work with it.

How to install Adminer in Laravel application


In this article we will install Adminer in Laravel 8 application