Laravel Tag System Tutorial Example

Admin   Laravel   1046  2021-03-15 21:30:08

Hello Friend,

If you are writing posts for your blog or your websites then you must have to need tagged system in you website. If you write post for technology then you create tags something like that technology name, technology types, technology features etc. So if you want to add tags in your posts or blog then you can use this tag systems.

In this post,I will show you how to generate tags system in laravel application using rtconner/laravel-tagging composer package. Here I will create posts table and then install laravel-tagging package. So When you create new posts or blog then you can add tags that you want on each posts or blog.

I am use bootstrap tagsinput js library for input tags that way it's awesome layout for inputs tags .

Here I will give you full example for laravel tags system tutorial example. So Let's follow bellow step by step.

Step 1 : Install Laravel Fresh App

In first step, You have to need laravel project. You can install laravel fresh project using bellow composer command:


composer create-project --prefer-dist laravel/laravel blog

Step 2 : Setup Database Configuration

After successfully install laravel app thenafter configure databse setup. We will open ".env" file and change the database name, username and password in the env file.








Step 3 : Install rtconner/laravel-tagging Composer Package

In this step, we require to install "rtconner/laravel-tagging" composer package for add tags. so let's run bellow command:


composer require composer require rtconner/laravel-tagging

After successfully install package, open config/app.php file and add service provider.



'providers' => [





After register provider, we require to make public configuration and then run migration for tags tags, so let's run bellow both command.


php artisan vendor:publish --provider="Conner\Tagging\Providers\TaggingServiceProvider"

For run migration:


php artisan migrate

Step 4 : Create posts table and model

In this step, You have to create posts table in your database. Run bellow command to create migration and modal So lets open terminal and run bellow command:


php artisan make:modal Post -m

After run above command, you will see a migration file in following path database/migrations and you have to simply put following code in migration file to create posts table.



use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration



* Run the migrations.


* @return void


public function up()


Schema::create('posts', function (Blueprint $table) {









* Reverse the migrations.


* @return void


public function down()





Run migration file and run following command:


php artisan migrate

After create 'posts' table thenafter changes in Post modal copy bellow code and put in the Post.php file.




namespace App;

use Illuminate\Database\Eloquent\Model;

use App\User;

class Post extends Model


use \Conner\Tagging\Taggable;

protected $fillable = ['title','tags','description'];


Step 5 : Add Route

Here We will create route for posts listing and create post. So let's open routes/web.php file and add following routes:



Route::get('posts', '[email protected]');

Route::post('posts', '[email protected]');

Step 6 : Create Controller

In this step, You can create Post Controller and add two method in post controller first method is create form and second method is store data:




namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Post;

class PostController extends Controller


public function create()


$posts = Post::all();

return view('posts',compact('posts'));


public function store(Request $request)


$this->validate($request, [

'title' => 'required',

'description' => 'required',

'tags' => 'required',


$input = $request->all();

$tags = explode(", ", $input['tags']);

$post = Post::create($input);


return back()->with('success','Post created successfully.');



Step 7 : Create View File

In Last step, let's create posts.blade.php(resources/views/posts.blade.php) for layout and we will display all posts and create posts form,so put following code:



<html lang="en">


<title>Laravel Tag System Tutorial Example</title>

<link rel="stylesheet" href="" integrity="sha256-aAr2Zpq8MZ+YA/D6JtRD3xtrwpEz2IqOS+pWD/7XKIw=" crossorigin="anonymous" />

<link rel="stylesheet" href="" integrity="sha512-xmGTNt20S0t62wHLmQec2DauG9T+owP9e6VU8GigI0anN7OXLip9i7IwEhelasml2osdxX71XcYm6BQunTQeQg==" crossorigin="anonymous" />

<script src=""></script>

<script src="" integrity="sha256-OFRAJNoaD8L3Br5lglV7VyLRf0itmoBzWUoM+Sji4/8=" crossorigin="anonymous"></script>

<script src="" integrity="sha512-VvWznBcyBJK71YKEKDMpZ0pCVxjNuKwApp4zLF3ul+CiflQi6aIJR+aZCP/qWsoFBA28avL5T5HA+RE+zrGQYg==" crossorigin="anonymous"></script>

<script src="" integrity="sha512-KT0oYlhnDf0XQfjuCS/QIw4sjTHdkefv8rOJY5HHdNEZ6AmOh1DW/ZdSqpipe+2AEXym5D0khNu95Mtmw9VNKg==" crossorigin="anonymous"></script>

<style type="text/css">


background-color: #17a2b8;


.label {

display: inline-block;

padding: .25em .4em;

font-size: 75%;

font-weight: 700;

line-height: 1;

text-align: center;

white-space: nowrap;

vertical-align: baseline;

border-radius: .25rem;

transition: color .15s ease-in-out,background-color .15s ease-in-out,

border-color .15s ease-in-out,box-shadow .15s ease-in-out;





<div class="row mt-5 mb-5 mr-0">

<div class="col-md-10 offset-1">

<div class="card">

<div class="card-header bg-info text-white">

<h4>Laravel Tag System Tutorial Example - </h4>


<div class="card-body">


<div class="alert alert-success">

{{ Session::get('success') }}






<form method="POST" action="{{ route('') }}">


<div class="form-group">

<label>Title : <span class="text-danger">*</span></label>

<input type="text" name="title" class="form-control">

@if ($errors->has('title'))

<span class="text-danger">{{ $errors->first('title') }}</span>



<div class="form-group">

<label>Description : <span class="text-danger">*</span></label>

<textarea class="form-control" name="description"></textarea>

@if ($errors->has('description'))

<span class="text-danger">{{ $errors->first('description') }}</span>



<div class="form-group">

<label>Tags : <span class="text-danger">*</span></label>


<input type="text" data-role="tagsinput" name="tags" class="form-control tags">


@if ($errors->has('tags'))

<span class="text-danger">{{ $errors->first('tags') }}</span>



<div class="form-group">

<button class="btn btn-success store-data btn-sm">Save</button>




@foreach($posts as $post)

<div class="post-section">

<div class="post-title">

<h4>{{ $post->title }}</h4>


<div class="post-description">

<p style="margin-bottom: 5px !important;">{{ $post->description }}</p>


<div class="post-tags mb-4">

<strong>Tags : </strong>

@foreach($post->tags as $tag)

<span class="badge badge-info">{{$tag->name}}</span>












Now we are ready to run our example so run bellow command for quick run:


php artisan serve

Now you can open bellow URL on your browser:



you can get more information about "rtconner/laravel-tagging" package from here :


Output :

It will help you...