Laravel 8 Add Social Media Share Buttons

Share this Article
Reading Time: 6 minutes
1,079 Views

Inside this article we will see How to add social media share buttons in laravel 8. Social share media button means the button through which we can share content to social platforms like Facebook, Twitter, Instagram etc.

This tutorial will guide you to learn laravel 8 add social media share buttons into a great detail. This is very interesting to see and also super easy to implement.

Learn More –

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Learn Composer Installation Steps. Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.


Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=root

Create Model & Migration

Open project into terminal and run this command to create model & migration file.

$ php artisan make:model Post -m

It will create two files –

  • Model – Post.php at /app/Models folder
  • Migration file – 2021_07_25_123805_create_post_table.php at /database/migrations folder.

Open Migration file and write this complete code into it.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string("title", 120);
            $table->string("url", 225);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

Open model file Post.php and write this complete code into it.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

    public $timestamps = false;
}

Run Migration

Next, we need to create tables inside database.

$ php artisan migrate

This command will create tables inside database.


Dummy Data for Application

Here, you need to copy the given mysql query and run into sql tab of phpmyadmin.

--
-- Dumping data for table `posts`
--

INSERT INTO `posts` (`id`, `title`, `url`) VALUES
(1, 'Concept of Route Middleware in Laravel 8 Tutorial', 'https://onlinewebtutorblog.com/concept-of-route-middleware-in-laravel-8-tutorial/'),
(2, 'Concept of Trait in Laravel 8 Tutorial with Example', 'https://onlinewebtutorblog.com/concept-of-trait-in-laravel-8-tutorial-with-example/'),
(3, 'Create Signature Pad & Save Using jQuery in Laravel 8', 'https://onlinewebtutorblog.com/create-signature-pad-save-using-jquery-in-laravel-8/'),
(4, 'File or Image Upload Using Livewire Laravel 8 Tutorial', 'https://onlinewebtutorblog.com/file-or-image-upload-using-livewire-laravel-8-tutorial/');

You will see something like this after this test data insertion.


Create Controller

Next, we need to create a controller file.

$ php artisan make:controller SiteController

It will create a file SiteController.php at /app/Http/Controllers folder.

Open SiteController.php and write this complete code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;

class SiteController extends Controller
{
    public function sharePosts()
    {
        $shareButtons = \Share::page(
            'https://onlinewebtutorblog.com/',
            'What you are writing, just share to world to learn!!',
        )
        ->facebook()
        ->twitter()
        ->linkedin()
        ->telegram()
        ->whatsapp()        
        ->reddit();
  
        $posts = Post::get();

        return view("share-post", compact('shareButtons', 'posts'));
    }
}

Create Blade Layout File

Go to /resources/views folder and create a file with name share-post.blade.php

Open share-post.blade.php and write this complete code into it.

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Laravel 8 Add Social Media Share Buttons</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
    <style>
        #social-links li {
            display: inline-block;
            padding: 4px;
            text-align: center;
            list-style: none;
        }

    </style>
</head>

<body>

    <div class="container">
        <h3>Laravel 8 Add Social Media Share Buttons</h3>
        <div class="panel panel-primary">
            <div class="panel-heading">Laravel 8 Add Social Media Share Buttons</div>
            <div class="panel-body">
                
                {!! $shareButtons !!}

                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>#ID</th>
                            <th>Post</th>
                            <th>URL</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($posts as $post)
                            <tr>
                                <td>
                                    {{ $post->id }}
                                </td>
                                <td>
                                    {{ $post->title }}
                                </td>
                                <td>
                                    {!! Share::page($post->url, $post->title)->facebook()->twitter()->whatsapp() !!}
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</body>

</html>

Create Route

Open web.php from /routes folder and add this route into it.

# Add this to header
use App\Http\Controllers\SiteController;

//...

Route::get("share-posts", [SiteController::class, "sharePosts"]);

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

URL: http://127.0.0.1:8000/share-posts

We hope this article helped you to Laravel 8 Add Social Media Share Buttons in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here