online software programming courses

Dynamic SEO meta-tags in Laravel website with example code

SEO has been a headache for most coders and marketers. There are many factors for good SEO ranking. On-page SEO plays the most important role within google ranking. But most beginners ignore or don't know some of the most important techniques for on-page SEO. When I first started SEO I did not know them either and my google ranking was very poor.

meta description tag

Do you know how the best practices for SEO that will drive traffic to your page? After doing SEO for many months, I found 5 important strategies which will rank you high and double the traffic if you do them very carefully.

In this article, I will list those five steps or techniques important and share the code example with you for meta keywords and descriptions so that, you can create them dynamically and store in your database. See these 5 strategies for effective SEO below

1. Unique content writing at least 500-800 words, the more the better

2. SEO friendly slug url

3. Title tag

4. Meta description tag

5. Meta keywords tag

 

Steps for generating dynamic meta tags in Laravel

1. Add a form in your partials with meta tags

2. Submit it to the server database using the controller

3. Use @section directive in the front end partials

4. Use @yield directive in the master template 

1. Add a form in your partials with meta tags

For dynamically generating meta description and keywords for you each page, your need to add a form that includes these two fields. You can do them easily with the below code

<form method="POST" action="/tasks" enctype="multipart/form-data">
		{{csrf_field()}}
                 .............................................
                ..............................................
		<div class="form-group">
		    <label >Meta keywords</label>
                    <input type="text" class="form-control"  name="meta_keywords">
               </div>
	       <div class="form-group">
			<label >Meta description</label>
                        <input type="text" class="form-control"  name="meta_description">
		</div>
               ...........................................
              ..............................................
               <button type="submit" class="theme-btn">Submit</button>
</form>

The above code will take the meta keywords and description in a text input field. 

Route::post('/tasks', 'PostTasksController@store');

Then I used the tasks route to send the meta description and keywords to a controller method. In my case, I called a method name store. You can use any method you want.

dynamic seo meta-tags laravel

See the photo above to have a better understanding of how the input fields look like.

2. Submit it to the server database using the controller

    public function store(Request $request)
    {
        $this->validate($request, array(
            .....................................
            'meta_keywords' =>'required|max:1000|min:5',
            'meta_description'=>'required|max:1000|min:5'
            
        ));

       $task = new Task();
       .....................................
       .....................................

       $task->meta_keywords = $request->input('meta_keywords');
       $task->meta_description = $request->input('meta_description');
       .....................................
       $task->save();

       return redirect('/posts');
        
    }

The above code saves the descriptions and keywords into the database and redirects to posts page. I suggest doing the validation. After all, too short or too long can cause CTR(click-through-rate). 

Meta tag description should be between 50-155 characters. If it's more than it's not readable and google may not show in the search result. 

3. Use @section directive in the front end partials

Show the meta tags in partials could be a little tricky, at least at the beginning. But it's actually easy.  You can use a section directive in the partials to show the data that you retrieved from the server for a certain post id. See how you can write section directives both for meta keywords and description.

@section('meta_description')
        {{$page->meta_description}}
@endsection
@section('meta_kewords')
         {{$page->meta_keywords}}
@endsection

You can see that our @section directives names are meta_description and meta_keywords.

4. Use @yield directive in the master template 

@yield is another directive in laravel which is used for sending data from @section directive to front end mater partial or your main layout template.

<meta name ="description", content="@yield('meta_description', 'We build e-commerce site, management site, Android, iOS apps, and teach software development courses online.Laravel, Java, NodeJs, PHP technology.')">
<meta name ="keywords", content="@yield('meta_keywords', 'We build e-commerce site, management site, Android, iOS apps, and teach software development courses online.Laravel, Java, NodeJs, PHP technology.')">

You need to call the @yield directive within your content attribute. The first parameter of yield function takes the @section directive name which we named earlier. The second parameter is a fallback, if you don't have the value for the tags.