Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Share URL on social media using package in Laravel 98765

How to Share URL on social media using package in Laravel 9/8/7/6/5 ?

Aman Jain, March 15, 2022October 4, 2022

In this tutorial i will show you to Share URL on social media using package in Laravel. Laravel provides several packages to make the task easy, In the same way we can add third party package to share the URLs on multiple social media using the package jorenvanhocht/laravel-share.

This package allow us to share the url on multiple platform as below

  • Facebook
  • Twitter
  • LinkedIn
  • WhatsApp
  • Reddit
  • Telegram
Screenshot 2022 03 15 at 10.27.58 PM
Share URL on social media using package in Laravel

jorenvanhocht/laravel-share provides multiple option configure the package and it also have some dependencies like fontawesome and jQuery.

Sharing a url can be a requirements of each and every page, and it can be a pain if we are doing it manually so using this package we can make easy.

So let’s begin the tutorial Share URL on social media using package in Laravel step by step

Step 1 : Install the package

I assume the you have already installed the laravel and basic connection of it like database connection and composer.

Now install the package using composer in laravel root directory, open the terminal in laravel root directory and run below command

composer require jorenvanhocht/laravel-share

And then run composer update

composer update

Step 2 : Add Service provider and alias

Most of the packages comes with laravel package discovery enabled and automatic add service provider to packages built after laravel 5.5 so if your laravel version is greater then 5.5 then you can skip this step.

Search for providers key and Add service provider in config\app.php

'providers' => [
        // ...
        Jorenvh\Share\Providers\ShareServiceProvider::class,
    ]

Also find alias key and add as below

aliases' => [
        // ...
        'Share' => Jorenvh\Share\ShareFacade::class,
    ]

Step 3 : Publish config to add configuration

Now, publish the config of package so we can change the config according to need. Run the below command in project directory

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

This will publish the laravel-share.php config file to your config folder, share.js in public/js/ and laravel-share.php in your resources/lang/vendor/en/ folder

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Services
    |--------------------------------------------------------------------------
    |
    | Specify the base uri for each service.
    |
    |
    |
    */

    'services' => [
        'facebook' => [
            'uri' => 'https://www.facebook.com/sharer/sharer.php?u=',
        ],
        'twitter' => [
            'uri' => 'https://twitter.com/intent/tweet',
            'text' => 'Default share text',
        ],
        'linkedin' => [
            'uri' => 'https://www.linkedin.com/sharing/share-offsite', // oud: http://www.linkedin.com/shareArticle
            'extra' => ['mini' => 'true'],
        ],
        'whatsapp' => [
            'uri' => 'https://wa.me/?text=',
            'extra' => ['mini' => 'true'],
        ],
        'pinterest' => [
            'uri' => 'https://pinterest.com/pin/create/button/?url=',
        ],
        'reddit' => [
            'uri' => 'https://www.reddit.com/submit',
            'text' => 'Default share text',
        ],
        'telegram' => [
            'uri' => 'https://telegram.me/share/url',
            'text' => 'Default share text',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Font Awesome
    |--------------------------------------------------------------------------
    |
    | Specify the version of Font Awesome that you want to use.
    | We support version 4 and 5.
    |
    |
    */

    'fontAwesomeVersion' => 5,
];

Step 4 : Create routes

Add appropriate routes to connect our URL to controller therefore open routes/web.php and add below routes

<?php
use Illuminate\Support\Facades\Route;
use \App\Http\Controllers\PostController;


Route::get('/share-post',[PostController::class, 'share']);

Here we created one route only that means you can create it anywhere but we are using it only our demo purpose.

Step 4 : Create Controller

Now, create the controller as we have mentioned in routes is PostController and also create three methods create, store and refreshCaptcha so create a file in app\Http\Controllers\PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class PostController extends Controller
{

    public function share()
    {
        return view('post.sahre-post');
    }
}

Step 5 : Create view

We have create the controller and now we will create the view to show the button for sharing on social media

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>Readerstacks Share URL on social media like Facebook, Whatsapp, Twitter, LinkedIn in Laravel </title>

  <link href="//netdna.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.1/css/all.min.css" integrity="sha512-9my9Mb2+0YO+I4PUCSwUYO7sEK21Y0STBAiFEYoWtd2VzLEZZ4QARDrZ30hdM1GlioHJ8o8cWQiy8IAb1hy/Hg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>

<body class="antialiased">
  <div class="container">
    <!-- main app container -->
    <div class="readersack">
      <div class="container">
        <div class="row">
          <div class="col-md-6 offset-md-3">
            <h3>  Share URL on social media like Facebook, Whatsapp, Twitter, LinkedIn in Laravel  - Readerstacks</h3>
          
           {!!Share::page('https://readerstacks.com', 'Share title',["target"=>"_blank"])
            ->facebook()
            ->twitter()
            ->linkedin('Extra linkedin summary can be passed here')
            ->whatsapp();
          !!}
             
          </div>
        </div>
      </div>
    </div>
    <!-- credits -->
    <div class="text-center">
      <p>
        <a href="#" target="_top"> Share URL on social media like Facebook, Whatsapp, Twitter, LinkedIn in Laravel  - Readerstacks</a>
      </p>
      <p>
        <a href="https://readerstacks.com" target="_top">readerstacks.com</a>
      </p>
    </div>
  </div>
</body>
</html>

Here, we used {!! {!!Share::page() !!} to show the social media icons

{!!Share::page('https://readerstacks.com', 'Share title',["class"=>"social"])
            ->facebook()
            ->twitter()
            ->linkedin('Extra linkedin summary can be passed here')
            ->whatsapp();
          !!}

Open share url in new tab or popup

you can open the share url in new tab or in window popup using including script file and jQuery in page as below

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs=" crossorigin="anonymous"></script>
<script src="{{ asset('js/share.js') }}"></script>

Now serve the project using

php artisan serve

and check the implementation.

Creating Facebook share link only

Share::page('http://jorenvanhocht.be')->facebook();

Also Read : How to Share Page to Facebook in Laravel PHP ?

Creating Twitter share link only

Share::page('http://jorenvanhocht.be', 'Your share text can be placed here')->twitter();

Creating Reddit share link only

Share::page('http://jorenvanhocht.be', 'Your share text can be placed here')->reddit();

Creating LinkedIn share link only

Share::page('http://jorenvanhocht.be', 'Share title')->linkedin('Extra linkedin summary can be passed here')

Creating WhatsApp share link only

Share::page('http://jorenvanhocht.be')->whatsapp()

Also Read : How to Share/Send Message to WhatsApp in Laravel PHP ?

Creating Telegram share link only

Share::page('http://jorenvanhocht.be', 'Your share text can be placed here')->telegram();

Sharing the current url

Instead of manually passing an url, you can opt to use the currentPage function.

Share::currentPage()->facebook();

Related

Php Laravel Laravel 9 facebooklaravellinkdinphpsharetwitterwhatsapp

Post navigation

Previous post
Next post

Related Posts

Php How to Update Enum Field Value using Laravel Migration

How to Update Enum Field Value using Laravel Migration ?

August 13, 2022March 16, 2024

Sometime after add the values in enum field we wanted to Update Enum Field Value using Laravel Migration so that we can work according to requirements. In our last article How to Create Enum Field in Laravel Migration ? we learnt to create the enum and today we are give…

Read More

How to find php.ini file location ?

August 24, 2021February 22, 2024

Sometime we installed multiple version of php on our system so it will get difficult to find php.ini file location . Let’s start with easiest way. Steps to find the php ini file on dedicated, localhost and ssh based enabled server. Then run this file in browser. As we can…

Read More
Php Laravel custom login and register

Laravel 8 Custom login and registration with example

December 3, 2021December 3, 2021

In Laravel 8 there is multiple ways to implement the login and registration like Laravel provides its own auth with packages Jetstream, passport,sanctum, breeze and fortify. These all packages are easy to install and configure but sometimes our application requirement and design patterns are different or we can say we…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version