Laravel Shareable Trait

Dennis Smink
Jun 25, 2018 · 3 min read
Code example of the Shareable Trait

I always hated to manually write sharing URL’s for social media for my models, so I created a simple trait for myself that made this easier. This trait supports:

  • Facebook
  • Google
  • Twitter
  • Pinterest
  • WhatsApp

Lets say you have a News.php model and want to easily generate share URL’s for this, just adding a Trait and being able to call 1 method on that model would be lovely, won’t it? We’ll I have just got just that Trait class for you:

<?php

namespace App\Traits;

trait Shareable
{
public function getShareUrl($type = 'facebook')
{
$url = $this->{array_get($this->shareOptions, 'url')} ? $this->{array_get($this->shareOptions, 'url')} : url()->current();

if ($type == 'facebook') {
$query = urldecode(http_build_query([
'app_id' => env('FACEBOOK_APP_ID'),
'href' => $url,
'display' => 'page',
'title' => urlencode($this->{array_get($this->shareOptions, 'columns.title')})
]));

return 'https://www.facebook.com/dialog/share?' . $query;
}

if ($type == 'twitter') {
$query = urldecode(http_build_query([
'url' => $url,
'text' => urlencode(str_limit($this->{array_get($this->shareOptions, 'columns.title')}, 120))
]));

return 'https://twitter.com/intent/tweet?' . $query;
}

if ($type == 'whatsapp') {
$query = urldecode(http_build_query([
'text' => urlencode($this->{array_get($this->shareOptions, 'columns.title')} . ' ' . $url)
]));

return 'https://wa.me/?' . $query;
}

if ($type == 'linkedin') {
$query = urldecode(http_build_query([
'url' => $url,
'summary' => urlencode($this->{array_get($this->shareOptions, 'columns.title')})
]));

return 'https://www.linkedin.com/shareArticle?mini=true&' . $query;
}

if ($type == 'pinterest') {
$query = urldecode(http_build_query([
'url' => $url,
'description' => urlencode($this->{array_get($this->shareOptions, 'columns.title')})
]));

return 'https://pinterest.com/pin/create/button/?media=&' . $query;
}

if ($type == 'google') {
$query = urldecode(http_build_query([
'url' => $url,
]));

return 'https://plus.google.com/share?' . $query;
}
}
}

Apply this trait to your model, in my case News.php:

class News extends Model
{
use
Shareable;

Now lets set the ‘title’ column inside the News.php model;

protected $shareOptions = [
'columns'
=> [
'title'
=> 'database_title_column'
],
'url' => 'url'
]
;

The value for the key database_title_column is the column name in your database that this Trait should use for title sharing.

For the URL part, you can create a attribute helper for this:

public function getUrlAttribute()
{
return
route('news.show', $this->slug);
}

If you do not specify a URL (passing null) it will get the current URL you are on.

Total code:

class News extends Model
{
use
Shareable;

protected $shareOptions = [
'columns'
=> [
'title'
=> 'title'
]
,
'url' => null
]
;

public function getUrlAttribute()
{
return
route('news.show', $this->slug);
}
}

Now lets start using this, lets grab a record:

$news = News::first();

Now lets generate a URL:

$news->getShareUrl();

This will default generate a URL for facebook, but if you want to you can also generate for other social media:

$news->getShareUrl('whatsapp');
$news->getShareUrl('twitter');
$news->getShareUrl('pinterest');
$news->getShareUrl('google');

I hope this was of any use for you, and if you have any more social media that we can add let me know!

Thank you for reading!

Note: please bare in mind that I am a Dutch developer, English is not my native language so this article could contain any grammatical errors.

Dennis Smink

Written by

Laravel enthusiast, owner of https://ploi.io, personal website: https://dennissmink.nl