Laravel 8 Vue JS Owl Carousel Slider Example Tutorial
In this article, we will explain to you how to create the Vue js Owl carousel slider example in laravel 8(Laravel 8 Vue JS Owl Carousel Slider Example Tutorial). We can easily create a carousel slider using the vue js npm package. so in this example, we will install the npm vue-owl-carousel package.
Sometimes, we need a slider to share our story or portfolio on the website because the slider’s help to the user can easily understand our story.
Now, let’s follow the below steps for how to create an owl carousel slider.
Overview
Step 1: Install laravel
Step 2: Setting Database Configuration
Step 3: Create Table using migration
Step 4: Create a Model and Controller
Step 5: Install Vue
Step 6: Create Route
Step 7: Create Component and update app.js
Step 8: Update Blade Files
Step 9: Run Our Laravel Application
Step 1: Install laravel
Install the laravel using the below command.
1 | composer create-project --prefer-dist laravel/laravel laravel8_vuejs_slider |
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_vuejs_slider) 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 bookings table migration.
Create The table using the below command.
1 | php artisan make:migration create_sliders_table --create=sliders |
After complete migration. we need the below changes in the database/migrations/create_bookings_table file.
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 | <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateSlidersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('sliders', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('title'); $table->string('url'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('sliders'); } } ?> |
Run the below command. after the changes above file.
1 | php artisan migrate |
Step 4: Create a Model and Controller
Now, We will create the controller and model using the below command and paste the below code into this controller.
1 | php artisan make:controller SliderController |
1 | php artisan make:model Slider |
SliderController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Slider; use Redirect,Response; class SliderController extends Controller { public function index() { $sliders = Slider::get(); return response()->json(["sliders" => $sliders]); } } ?> |
Slider.php
1 2 3 4 5 6 7 8 9 10 11 12 | <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Slider extends Model { use HasFactory; protected $fillable = ['title','url']; } ?> |
Step 5: Install Vue
In this step, go to the project directory using the command prompt. after then we will install Vue js in laravel 8 and run vue-owl-carousel package.
1 2 3 4 | php artisan preset vue npm install npm i -s vue-owl-carousel |
Step 6: Create Route
Add the following route code in the “routes/web.php” file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php use App\Http\Controllers\SliderController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('sliders', [SliderController::class, 'index']); ?> |
Step 7: Create Component and update app.js
resources/js/components/SliderComponent.vue
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 35 | <template> <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <carousel> <img class="img-fluid" v-for="(img, index) in images" :src="img.url" :key="index"> </carousel> </div> </div> </div> </template> <script> import carousel from "vue-owl-carousel"; export default { components: { carousel }, name: "Owl Carousel", props: { msg: String }, data() { return { images: [] } }, created() { this.axios .get('http://localhost:8000/sliders') .then(response => { this.images = response.data; }); } }; </script> |
resources/js/app.js
1 2 3 4 5 6 | require('./bootstrap'); window.Vue = require('vue'); Vue.component('slider-component', require('./components/SliderComponent.vue').default); const app = new Vue({ el: '#app', }); |
Step 8: Update Blade Files
So finally, first we need to create the app.blade.php file in the “resources/views/layouts/” and Update the welcome.blade.php file in the “resources/views/” directory. so you can see the below code.
resources/views/layouts/app.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>{{ config('app.name', 'Laravel') }}</title> <link href="{{ mix('css/app.css') }}" rel="stylesheet"> <title>Laravel 8 Vue JS Owl Carousel Slider Example Tutorial - XpertPhp</title> </head> <body> <div id="app"> <div class="py-4"> @yield('content') </div> </div> <script src="{{ mix('js/app.js') }}" defer></script> </body> </html> |
welcome.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Laravel 8 Vue JS Owl Carousel Slider Example Tutorial</div> <div class="card-body"> <slider-component></slider-component> </div> </div> </div> </div> </div> @endsection |
Step 9: Run Our Laravel Application
We can start the server and run this example using the below command.
1 2 3 | npm run dev //or npm run watch |
Now we will run our example using the below Url in the browser.
1 | http://127.0.0.1:8000/users |
Read Also
Laravel 8 Implement Custom Flash Message With Example
Laravel 8 Livewire File Upload From Scratch
Laravel 8 Livewire DataTable Example Tutorial