Laravel 8 Dynamic Bootstrap Tabs Example Tutorial
Today we are telling you how to make Dynamic Bootstrap Tabs Example in laravel 8. if you want to learn dynamic bootstrap tabs in laravel then you can follow our tutorial.
Sometimes, we see a tabs system on many e-commerce websites. we have given a simple example of how can make it in laravel. so you can follow the below example.
Overview
Step 1: Install Laravel 8
Step 2: Setting Database Configuration
Step 3: Create Table using migration
Step 4: Create Route
Step 5: Create a Model and Controller
Step 6: Create View File
Step 7: Run Our Laravel Application
Step 1: Install Laravel 8
We are going to install laravel 8, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command for laravel 8 install.
1 | composer create-project --prefer-dist laravel/laravel laravel8_bootstrap_tabs |
Step 2: Setting Database Configuration
After the complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.
1 2 3 4 5 6 | DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name(laravel8_bootstrap_tabs) DB_USERNAME=Enter_Your_Database_Username(root) DB_PASSWORD=Enter_Your_Database_Password(root) |
Step 3: Create Table using migration
Now, We need to create a migration. so we will below command using create the products and categories tables.
1 2 | php artisan make:migration create_products_table --create=products php artisan make:migration create_categories_table --create=categories |
After complete migration. we need the below changes in the database/migrations/create_products_table.php and database/migrations/create_categories_table.php files.
database/migrations/create_products_table.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateProductsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('products', function (Blueprint $table) { $table->id(); $table->foreignId('category_id'); $table->string('name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('products'); } } ?> |
database/migrations/create_categories_table.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateCategoriesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('categories', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('categories'); } } ?> |
Run the below command. after the changes above file.
1 | php artisan migrate |
Step 4: Create Route
We have to need put below student resource route in routes/web.php
1 2 3 4 5 | Route::namespace('App\Http\Controllers')->middleware('guest')->group(function () { Route::get('/', 'ProductController@index'); }); |
Step 5: Create a Model and Controller
Here below command help to create the controller and model.
1 2 3 | php artisan make:controller ProductController php artisan make:model Product php artisan make:model Category |
app/Models/Product.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Product extends Model { use HasFactory; public function products() { return $this->belongsTo(Category::class); } } |
app/Models/Category.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Category extends Model { use HasFactory; public function products() { return $this->hasMany(Product::class); } } |
ProductController.php
1 2 3 4 5 6 7 8 9 10 11 12 | namespace App\Http\Controllers; use App\Models\Category; use Illuminate\Http\Request; class ProductController extends Controller { public function index() { $category = Category::with('products')->get(); return view()->exists('welcome') ? view('welcome',compact('category')) : ''; } } |
Step 6: Create View File
So finally, first we need to update the welcome.blade.php file in the “resources/ views/” directory.
welcome.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <!DOCTYPE html> <html lang=""> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel 8 Dynamic Bootstrap Tabs Example Tutorial - XpertPhp</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> </head> <body> <div role="tabpanel"> <ul class="nav nav-tabs" role="tablist"> @foreach ($category as $item) <li role="presentation" class="{{ $item->id == 1 ? 'active' : '' }}"> <a href="#home{{ $item->id }}" aria-controls="home" role="tab" data-toggle="tab">{{ $item->name }}</a> </li> @endforeach </ul> <div class="tab-content"> @foreach ($category as $item) <div role="tabpanel" class="tab-pane {{ $item->id == 1 ? 'active' : '' }}" id="home{{ $item->id }}" class="active"> <ul> @foreach ($item->products as $element) <li>{{ $element->name}}</li> @endforeach </ul> </div> @endforeach </div> </div> </body> </html> |
Step 7: Run Our Laravel Application
We can start the server and run this example using the below command.
1 | php artisan serve |
Now we will run our example using the below Url in the browser.
1 | http://127.0.0.1:8000/ |
Read Also
Laravel 6 CRUD (Create Read Update Delete) Tutorial For Beginners
Laravel 7 CRUD Operation With Ajax Example
Laravel 6 CRUD Operation With Ajax Example
Laravel 6 Pagination Example Tutorial
Laravel 7 Pagination Example Tutorial