1. Go to this page and download the library: Download fomvasss/laravel-meta-tags library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fomvasss / laravel-meta-tags example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Fomvasss\LaravelMetaTags\Traits\Metatagable;
class Article extends Model
{
use Metatagable;
//...
}
namespace App\Http\Controllers;
use MetaTag;
class ArticleController extends Controller
{
public function index()
{
$articles = \App\Model\Article::paginate();
MetaTag::setTags([
'title' => 'Article index page',
'description' => 'It is article index page',
]);
return view('index', compact('articles'));
}
public function store(Request $request)
{
// create entity
$article = \App\Model\Article::create($request->only([
//.. article data
]));
// create meta tag for entity
$article->metaTag()->create($request->only([
//.. meta tags fields
]));
}
public function show($id)
{
$article = \App\Model\Article::findOrFail($id);
// Set tags for showing
MetaTag::setEntity($article)
->setDefault([
'title' => $article->title, // if empty $article->metaTag->title - show this title
])->setTags([
'seo_text' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit',
'h1' => $article->title,
]);
return view('stow', compact('article'));
}
public function search(Request $request)
{
$articles = \App\Model\Article::bySearch($request->q)
->paginate();
// Set tags for showing
MetaTag::setPath() // if argument `setPath()` is empty (or not set) - path = `request()->path()`
->setDefault([
'title' => 'Search page',
'robots' => 'noindex',
'og_title' => 'Search page OG',
'twitter_title' => 'Search page Twitter',
'canonical' => 'page/search',
]);
return view('index', compact('articles'));
}
}