Laravel 9 Social Media Share Buttons Integration Example

Hello Artisan,

In this example, I will show you how to make share button in laravel 9. You know that, in web applications, we need to share links. So we need a share button for sharing content on social media.

To create this how to share link in laravel, I will use jorenvanhocht/laravel-share composer package for adding social media sharing buttons in laravel project. This package provides Facebook Twitter LinkedIn, Reddit, Instagram, etc.

Let's see how to create and use laravel social share package and use it to laravel 9 share post on social media:

 

Step 1: Install Package

In the first step, we need to install jorenvanhocht/laravel-share composer package so let's use below command to install:

composer require jorenvanhocht/laravel-share

 

You can publish the package vendor by running the following command:

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

 

Step 2: Add Route

In this step, we need to create some routes for the social share example view.

routes/web.php

use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\SocialShareController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('social-share', [SocialShareController::class, 'index']);

 

Step 3: Create Controller

In this step, we need to create SocialShareController and add the following code to that file:

app/Http/Controllers/SocialShareController.php

namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Post;
  
class SocialShareController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $shareButtons = \Share::page(
            'https://www.laravelia.com',
            'Your share text comes here',
        )
        ->facebook()
        ->twitter()
        ->linkedin()
        ->telegram()
        ->whatsapp()        
        ->reddit();
  
        $posts = Post::get();
  
        return view('socialshare', compact('shareButtons', 'posts'));
    }
}

 

Step 4: Create Blade Files

Now, we need to create blade files for social share. so let's create one-by-one files:

here will the code for navbar links:

resources/views/socialshare.blade.php

 

Read also: Google Map Point A to Point B Direction Example

 

Hope it can help you.

 

#laravel #laravel-9x