In this tutorial, we will see how to generate tags system in laravel application using rtconner/laravel-tagging composer package.
Given below is the full example for laravel tags system tutorial example.
Step 1 : Install Laravel Fresh App
composer create-project --prefer-dist laravel/laravel blog
Step 2 : Setup Database Configuration
open ".env" file and change the database name, username and password in the env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name
DB_USERNAME=Enter_Your_Database_Username
DB_PASSWORD=Enter_Your_Database_Password
Step 3 : Install rtconner/laravel-tagging Composer Package
install "rtconner/laravel-tagging" composer package to add tags so run this command:
composer require composer require rtconner/laravel-tagging
config/app.php
'providers' => [
....
\Conner\Tagging\Providers\TaggingServiceProvider::class,
]
......
make public configuration and then run migration for tags so run the command given below:
php artisan vendor:publish --provider="Conner\Tagging\Providers\TaggingServiceProvider"
php artisan migrate
Step 4 : Create posts table and model
create posts table in your database. Run this command:
php artisan make:modal Post -m
Now run this following code after completion of above code successfully
<?php
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) {
$table->id();
$table->string('title');
$table->longText('description');
$table->text('tags');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Run following command:
php artisan migrate
app/Post.php
<?php
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
create route for posts listing and create post
routes/web.php
Route::get('posts', 'PostController@create');
Route::post('posts', 'PostController@store');
Step 6 : Create Controller
app/Http/Controllers/PostController.php
<?php
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);
$post->tag($tags);
return back()->with('success','Post created successfully.');
}
}
Step 7 : Create View File
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:
resources/views/posts.blade.php
<html lang="en">
<head>
<title>Laravel Tag System Tutorial Example</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha256-aAr2Zpq8MZ+YA/D6JtRD3xtrwpEz2IqOS+pWD/7XKIw=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.css" integrity="sha512-xmGTNt20S0t62wHLmQec2DauG9T+owP9e6VU8GigI0anN7OXLip9i7IwEhelasml2osdxX71XcYm6BQunTQeQg==" crossorigin="anonymous" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha256-OFRAJNoaD8L3Br5lglV7VyLRf0itmoBzWUoM+Sji4/8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.js" integrity="sha512-VvWznBcyBJK71YKEKDMpZ0pCVxjNuKwApp4zLF3ul+CiflQi6aIJR+aZCP/qWsoFBA28avL5T5HA+RE+zrGQYg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput-angular.min.js" integrity="sha512-KT0oYlhnDf0XQfjuCS/QIw4sjTHdkefv8rOJY5HHdNEZ6AmOh1DW/ZdSqpipe+2AEXym5D0khNu95Mtmw9VNKg==" crossorigin="anonymous"></script>
<style type="text/css">
.label-info{
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;
}
</style>
</head>
<body>
<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 - rathorji.in</h4>
</div>
<div class="card-body">
@if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
@php
Session::forget('success');
@endphp
</div>
@endif
<form method="POST" action="{{ route('posts.store') }}">
@csrf
<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>
@endif
</div>
<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>
@endif
</div>
<div class="form-group">
<label>Tags : <span class="text-danger">*</span></label>
<br>
<input type="text" data-role="tagsinput" name="tags" class="form-control tags">
<br>
@if ($errors->has('tags'))
<span class="text-danger">{{ $errors->first('tags') }}</span>
@endif
</div>
<div class="form-group">
<button class="btn btn-success store-data btn-sm">Save</button>
</div>
</form>
@if($posts->count())
@foreach($posts as $post)
<div class="post-section">
<div class="post-title">
<h4>{{ $post->title }}</h4>
</div>
<div class="post-description">
<p style="margin-bottom: 5px !important;">{{ $post->description }}</p>
</div>
<div class="post-tags mb-4">
<strong>Tags : </strong>
@foreach($post->tags as $tag)
<span class="badge badge-info">{{$tag->name}}</span>
@endforeach
</div>
</div>
@endforeach
@endif
</div>
</div>
</div>
</div>
</body>
</html>
Run this command for quick run:
php artisan serve
Run this URL on your browser:
http://localhost:8000/posts
you can get more information about "rtconner/laravel-tagging" package from here :
I hope this example helps you.