6.6 C
New York
Monday, January 30, 2023

Simple Tags System Example in Laravel

If you are writing article for your blog or your personal website then you must need to implement taggable system in your website. If you write article or post for food then you have to create tags like on foods name, foods type etc. You also maybe see on there are several blog or website available tags bellow the article. So if you want to add tags in your article and you are using laravel framework then you are on right place.

In this post, i will give you example of how to generate tags system in laravel 5, laravel 6, laravel 7 and laravel 8 application using rtconner/laravel-tagging composer package. here i will simple create “articles” table and then install laravel-tagging package. So when you create new article you can add tags that you want on each article. I also list of article with list of tags on each article. I use bootstrap tagsinput js library for input tags that way it’s amazing layout for input tags.

After complete full example you will get layout as like bellow screen shot preview:


Step 1 : Install Laravel

In first step, If you haven’t install laravel then we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

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

Step 2: 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 3: Create articles table and model

here you have to create articles table in your database. First create migration for articles table using Laravel 5 php artisan command, so first run bellow command:

php artisan make:migration create_articles_table

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 articles table.


use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreateArticlesTable extends Migration



* Run the migrations.


* @return void


public function up()


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








* Reverse the migrations.


* @return void


public function down()





Run migration file and run following command:

php artisan migrate

After create `articles` table, we should create model for articles table. Create file in following path app/Article.php and put bellow couple of code in Article.php file:



namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model


use \Conner\Tagging\Taggable;

protected $fillable = [ 'title', 'body' ];


Step 4: Add New Routes

Here we will create route for article listing and create post article. so open your routes/web.php file and add following route.


Route::get('article', 'ArticleController@index');

Route::post('article', 'ArticleController@store');

Step 5: Create ArticleController Controller

In this point, now we should create new controller as ArticleController. in this controller we write two method, index() and store(). Using this two method we handle both routes. So let’s create ArticleController like as bellow:



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Article;

class ArticleController extends Controller



* Show the application dashboard.


* @return \Illuminate\Http\Response


public function index()


$articles = Article::all();

return view('article',compact('articles'));



* Show the application dashboard.


* @return \Illuminate\Http\Response


public function store(Request $request)


$this->validate($request, [

'title' => 'required',

'body' => 'required',

'tags' => 'required',


$input = $request->all();

$tags = explode(",", $request->tags);

$article = Article::create($input);


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



Step 7: Create article.blade.php View

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


<!DOCTYPE html>



<title>Article Lists</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.css" />

<script src="http://demo.itsolutionstuff.com/plugin/jquery.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.js"></script>



<div class="container">

<h1>Add Article</h1>


<div class="alert alert-success">

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






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

{{ csrf_field() }}

<div class="form-group">


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

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

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



<div class="form-group">


<textarea class="form-control" name="body" placeholder="Body"></textarea>

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

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



<div class="form-group">



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

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

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



<div class="form-group">

<button class="btn btn-success btn-submit">Submit</button>



<h1>Article Lists</h1>


@foreach($articles as $key => $article)

<h3>{{ $article->title }}</h3>

<p>{{ $article->body }}</p>



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

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








Now we are ready to run our example so you can open bellow URL on your browser:


you can get more information about “rtconner/laravel-tagging” package from here : rtconner/laravel-tagging.

I hope it can help you…

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles