File: /var/www/html/orbidirectory.com/app/Models/Seo.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Seo extends Model
{
use HasFactory;
// List of fillable fields for SEO metadata
protected $fillable = [
'route_name',
'meta_title',
'meta_keywords',
'meta_description',
'canonical_tag',
'og_title',
'og_description',
'og_image',
'og_url',
'twitter_card',
'twitter_title',
'twitter_description',
'twitter_image',
];
/**
* Fetch SEO data for a specific route.
*
* @param string $route_name
* @return Seo|null
*/
public static function seo($route_name)
{
return Seo::where('route_name', $route_name)->first();
}
/**
* Get Open Graph (OG) title.
*
* @return string|null
*/
public function getOgTitleAttribute()
{
return $this->og_title ?? $this->meta_title;
}
/**
* Get Open Graph (OG) description.
*
* @return string|null
*/
public function getOgDescriptionAttribute()
{
return $this->og_description ?? $this->meta_description; // Default to meta_description if og_description is not set
}
/**
* Get Open Graph (OG) image.
*
* @return string|null
*/
public function getOgImageAttribute()
{
return $this->og_image ?? ''; // Return default if not set
}
/**
* Get Open Graph (OG) URL.
*
* @return string|null
*/
public function getOgUrlAttribute()
{
return $this->og_url ?? url()->current(); // Default to the current URL if og_url is not set
}
/**
* Get Twitter card title.
*
* @return string|null
*/
public function getTwitterTitleAttribute()
{
return $this->twitter_title ?? $this->meta_title; // Default to meta_title if twitter_title is not set
}
/**
* Get Twitter card description.
*
* @return string|null
*/
public function getTwitterDescriptionAttribute()
{
return $this->twitter_description ?? $this->meta_description; // Default to meta_description if twitter_description is not set
}
/**
* Get Twitter card image.
*
* @return string|null
*/
public function getTwitterImageAttribute()
{
return $this->twitter_image ?? ''; // Return default if not set
}
/**
* Get canonical tag for SEO.
*
* @return string|null
*/
public function getCanonicalTagAttribute()
{
return $this->canonical_tag ?? url()->current(); // Default to current URL if canonical_tag is not set
}
/**
* Generate the full meta tags as an array.
*
* @return array
*/
public function getMetaTags()
{
return [
'title' => $this->meta_title,
'description' => $this->meta_description,
'keywords' => $this->meta_keywords,
'canonical' => $this->canonical_tag,
'og_title' => $this->getOgTitleAttribute(),
'og_description' => $this->getOgDescriptionAttribute(),
'og_image' => $this->getOgImageAttribute(),
'og_url' => $this->getOgUrlAttribute(),
'twitter_card' => $this->twitter_card,
'twitter_title' => $this->getTwitterTitleAttribute(),
'twitter_description' => $this->getTwitterDescriptionAttribute(),
'twitter_image' => $this->getTwitterImageAttribute(),
];
}
}