Laravel 8 Tag system without duplicate Entries with Example
Hello developers, In this example, I will show you how to do Laravel Tag System without duplicate Entries Tutorial in Laravel 8. Select2 a customizable select box with support for searching and tagging.
Tagging systems have become a most popular way of categorizing items, posts and you can spot them in almost every application. From blog posts to E-Commerce websites, they all have tagging implementations. I have uploaded original tutorial and files are on Whalecoders
Laravel 8 Tag system without duplicate Entries with Example
#1: Create Laravel Project
composer create-project laravel/laravel tagsystem
cd tagsystem
#2: Add Laravel UI and Bootstrap Auth
composer require laravel/ui --dev
php artisan ui bootstrap --auth
npm install
npm run dev
#2: Create Database on XAMPP phpMyAdmin
Now navigate to http://localhost/phpmyadmin
And click on the Database tab. Now you should see the option to Create a Database and input field to enter the database name. Write the database name tagsystem and hit the ‘Create’ button. You will see a success message in a while.
From the list of tables, you can view your database. You are free to use this database wherever you like with default settings. By default the HostName is ‘localhost’, MySQL user is ‘root’ and have no password.
#3: Configure SQL Database
Open .env file and now we can setup database credentials.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tagsystem
DB_USERNAME=root
DB_PASSWORD=
#4: Build Post Model and Migration File
php artisan make:model Post -m
Post.php file will be created in App\Models Folder and also create a create_posts_table.php migration file in database/migrations folder.
Open create_posts_table.php and replace with below code
Schema::create('posts', function (Blueprint $table) {
$table->id('id');
$table->string('title');
$table->text('description');
$table->timestamps();
});
#5: Build Tag Model and Migration File
php artisan make:model Tag -m
Tag.php file will be created in App\Models Folder and also create a create_tags_table.php migration file in database/migrations folder.
Open create_tags_table.php and replace with below code
Schema::create('tags', function (Blueprint $table) {
$table->id('id');
$table->string('name');
$table->string('slug');
$table->timestamps();
});
#6: Build Pivot post_tag Migration File
php artisan make:migration create_post_tag_table --create=post_tag
It will create a create_post_tag_table.php migration file in database/migrations folder.
Open create_post_tag_table.php and replace with below code
Schema::create('post_tag', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')->constrained();
$table->foreignId('tag_id')->constrained();
$table->timestamps();
});
#7: Add trait in a model files.
Open Post.php file and add the below code
protected $fillable = [
'title',
'descritpion',
];public function tags()
{
return $this->belongsToMany('App\Models\Tag', 'post_tag', 'post_id', 'tag_id');
}
Open Tag.php file and add the below code
protected $fillable = [
'name',
'slug',
];
public function posts()
{
return $this->belongsToMany('App\Models\Tag', 'post_tag', 'tag_id', 'post_id');}
Save files and run migration
php artisan migrate
php artisan serve
For full tutorial and for Original Content link is Laravel 8 Tag system without duplicate Entries with Example