For getting much organic traffic from the search engine like Google, Bing, Yahoo etc on a website there are many ways but adding meta tags on webpages is the most important one. The SEO meta tags define your webpage content summary and information about your content that helps the search engine to show on SERP (Search Engine Results Pages). In Laravel, we can make these meta tags dynamically. You can generate tags using third party packages or from scratch. Here in this post, I'll show you step by step process for generating SEO meta tags in your Laravel website from scratch without using any packages. When we'll use meta tags into our webpages it'll show the SERP looks like below.
First, add some yield fields into your site template. Suppose your site template is template.blade.php
into resources/views/frontend directory. Let's add the yield fields.
<head>
<title>@yield('title','A default title')</title>
<meta name="keywords" content="@yield('meta_keywords','some default keywords')">
<meta name="description" content="@yield('meta_description','default description')">
<link rel="canonical" href="{{url()->current()}}"/>
</head>
N.B: Do not forget to change the default value according to your needs.
Now add two new fields into your posts/articles
table where you save your post.
meta_keywords
meta_description
Do markup for adding/updating meta tags into your post create and update form.
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Meta Tags</h3>
</div>
<div class="panel-body">
<div class="form-group">
<label>Keywords</label>
<input name="meta_keywords" class="form-control">
</div>
<div class="form-group">
<label>Description</label>
<input name="meta_description" class="form-control">
</div>
</div>
</div>
Now do the code for saving & updating in your controller.
public function store(Request $request){
// your previous code
$post->meta_keywords = $request->get('meta_keywords');
$post->meta_description = $request->get('meta_description');
$post->save();
}
public function update(Request $request){
// your previous code
$post->meta_keywords = $request->get('meta_keywords');
$post->meta_description = $request->get('meta_description');
$post->update();
}
Open the post details blade template and add the code given below. For example, our post details blade template file name is single.blade.php
@extends('frontend.template')
@section('title', $post->title)
@section('meta_keywords', $post->meta_keywords)
@section('meta_description', $post->meta_description)
@section('content')
<!--your post content -->
@endsection
We are done! Now we can add our meta keywords and meta description for every post dynamically and it will automatically generate SEO meta tags for us in every post details page. We can also add meta tags in our static pages by adding a section on those pages.
@extends('frontend.template')
@section('title', 'Contact Us')
@section('meta_keywords','contact,contact us')
@section('meta_description', 'Contact page of example.com')
@section('content')
<!--your page content -->
@endsection
Hopefully, this post will help you to add SEO meta tags in your Laravel website. If this step by step post helpful to you for learning dynamic SEO meta tags adding in Laravel website then share it with others so that it'll be helpful for them.