Laravel 10 Vue JS Owl Carousel Slider Example

Laravel 10 Vue JS Owl Carousel Slider Example

If you are developing an app using Laravel and Vue.js and need to show a Carousel Slider to slide images and text, you can use the “vue-owl-carousel” package by installing it with the npm command: npm i -s vue-owl-carousel.

This package provides a user-friendly solution for implementing a Carousel Slider in your application. It offers a range of customizable options and features that allow you to display and slide images and text smoothly.

In this tutorial, you will learn how to install and use npm i -s vue-owl-carousel to create carousel slider in laravel 10 vue js app

Laravel 10 Vue JS Owl Carousel Slider Example

  • Step 1: Setup Laravel 10 App
  • Step 2: Configure Database to Laravel App
  • Step 3: Create Model And Migration
  • Step 4: Install and Configure Vue Js and Owl-carousel
  • Step 5: Add Routes
  • Step 6: Pass data From Controller to the Carousel Components
  • Step 7: Implement the Carousel Slider
  • Step 8: Create Blade Views And Initialize Vue Components
  • Step 9: Run Development Server

Step 1: Setup Laravel 10 App

In this step, Open your temrinal or cmd(command prompt).

So, you need to execute the following command into it to install laravel latest application setup in your server:

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

Step 2: Configure Database to Laravel App

Once you have installed laravel web app. Now, you need to configure the database with the app.

So, Go to your project root directory and open .env file. Then set up the database credential in .env file as follow:

 DB_CONNECTION=mysql 
 DB_HOST=127.0.0.1 
 DB_PORT=3306 
 DB_DATABASE=here your database name here
 DB_USERNAME=here database username here
 DB_PASSWORD=here database password here

Step 3: Create Model And Migration

Next step, you need to execute the following command:

php artisan make:model Slider -m

This command will create one model name slider.php and also create one migration file for the sliders table.

Now open create_sliders_table.php migration file from database>migrations and replace up() function with following code:

<?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('path');
            $table->timestamps();
      });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('sliders');
    }
}

Next, migrate the table using the below command:

php artisan migrate

Next, Navigate to app/Models/Slider.php and update the following code into your Slider.php model as follow:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Slider extends Model
{
    use HasFactory;

    protected $guarded = [];

}

Step 4: Install and Configure Vue Js and Owl-carousel

Now, you need ot install and configure vue js and owl carousel. So, execute the following commands on terminal or command prompt to install Vue js and owl-carousel in your laravel app:

php artisan preset vue

Install all Vue dependencies:

npm install

After that, install owl carousel dependencies by using the below command:

npm i -s vue-owl-carousel

Step 5: Add Routes

Next step, go to routes folder and open web.php file and add the following routes into your file:

routes/web.php

use App\Http\Controllers\SliderController;

Route::get('slider', [SliderController::class, 'index']);

Step 6: Pass data From Controller to the Carousel Components

Next step, open your command prompt and execute the following command to create a controller by an artisan:

php artisan make:controller SliderController

After that, go to app\Http\Controllers and open SliderController.php file. Then update the following code into your SliderController.php file:

<?php 

namespace App\Http\Controllers;

use App\Models\Slider;

use Illuminate\Http\Request;

class SliderController extends Controller
{
    public function index(Request $request)
    {
        $sliderImg = Slider::get();
        return response()->json($sliderImg);
        
    }
}

Step 7: Implement the Carousel Slider

Next step, go to resources/assets/js/components folder and create a file called SliderComponent.vue.

Now, update the following code into your SliderComponent.vue components file:

<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.path" :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/slider')
          .then(response => {
              this.images = response.data;
          });
  }
};
</script>

Now open resources/assets/js/app.js and include the SliderComponent.vue component as follow:

require('./bootstrap');
window.Vue = require('vue');
Vue.component('slider-component', require('./components/SliderComponent.vue').default);
const app = new Vue({
    el: '#app',
});

Step 8: Create Blade Views And Initialize Vue Components

In this step, navigate to resources/views and create one folder named layouts. Inside this folder create one blade view file named app.blade.php file.

Next, Navigate to resources/views/layouts and open app.blade.php file. Then update the following code into your app.blade.php file as follow:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Laravel 10 Vue JS Owl Carousel Slider Example - Tutsmake.com</title>
    <script src="{{ asset('js/app.js') }}" defer></script>
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    @stack('fontawesome')
</head>
<body>
    <div id="app">
        <main class="py-4">
            @yield('content')
        </main>
    </div>
</body>
</html>
  

Next, Navigate to resources/views/ and open welocome.blade.php. Then update the following code into your welcome.blade.php file as follow:

@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 Vue JS Owl Carousel Slider Example</div>
                    
                <div class="card-body">
                  <slider-component></slider-component>
                </div>
                
            </div>
        </div>
    </div>
</div>
@endsection 

Step 9: Run Development Server

Execute the following command on terminal to start the development server:

npm run dev
or 
npm run watch

Conclusion

In this laravel vue js owl carousel slider example, you have learned how to use owl carousel slider with vue js for multiple items in laravel apps.

Recommended Laravel Posts

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *