How to Share Blog Posts to Social Media in Laravel 10?

Even though we all use the simple method to share our posts on social media as it makes things much easier. There is no special packages need to be installed on your system for this. In this post we would show you how to use Facebook’s approved SDK to post a link there from any website. All we would have a button ready to click to post on the blog that would send post to Facebook as they provide that feature so we can use it for contacting services or pushing posts on facebook.

For example if we take a look at the given url here, we can easily push our blog posts on Facebook directly with just a click.

https://www.facebook.com/postingr/postingr.php?u=https://laramatic.com

Using PHP to Push Posts on Facebook

The next we will show you to use that link would be used in HTML on the blog to execute it accordingly. Here is the HTML snippet for that:

<a target="_blank" href='https://www.facebook.com/postingr/postingr.php?u=https://laramatic.com' rel="noopener">Post to Facebook </a>

To finish the job we need to take few steps and make it work that way. Here are those steps which are necessary to take.

#1 Add Laravel Routes

We need to connect our URL to the controller for that we would add routes by using routes/web.php

use Illuminate\Support\Facades\Route;
use \App\Http\Controllers\ArticleController;

Route::get('/posting-article',[ArticleController::class, 'posting']);

You will notice we added a single route just to make the point

#2 Create a Controller

Now we need to create a controller as we have mentioned in the routes there is a ArticleController and do the three methods such as make, store and refreshCaptcha, therefore, we are going to add a file in

app\Http\Controllers\ArticleController.php

use Illuminate\Support\Facades\Validator;

class ArticleController extends Controller
{

public function posting()
{
return view('article.posting-article');
}
}

#3 Create a View

Next we are going to crate a view so that the share button can be used for pushing posts to facebook.

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

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>laramatic Post/Send Message to Facebook in Laravel PHP </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"/>


</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> Post Page to Facebook in Laravel PHP</h3>

                        <div class="row">

                            <div class="col-12">
                                
                                <a class="btn btn-danger" target="_blank" href='https://www.facebook.com/postingr/postingr.php?u=https://laramatic.com' rel="noopener">Post to Facebook </a>
                            </div>
                        </div>
   </div>
                </div>
            </div>
        </div>
        <!-- credits -->
        <div class="text-center">
            <p>
                <a href="#" target="_top" rel="noopener"> Post Page to Facebook in Laravel PHP - Laramatic</a>
            </p>
            <p>
                <a href="https://pakainfo.com" target="_top" rel="noopener">laramatic.com</a>
            </p>
        </div>
    </div>
</body>
</html>

This is all about creating your own personalised sharing button for social media in Laravel PHP without having to break much. It’s as simple as it’s shown here. You can add other social medias if you like.