How to use Laravel's local development server with Vue.js



Image not found!!

To use Laravel's local development server with Vue.js, you can follow these general steps:

  1. Create a New Laravel Project:

    • Install Laravel using Composer:

      bash
      composer create-project --prefer-dist laravel/laravel your-project-name
  2. Install Vue.js:

    • Laravel comes with Laravel Mix, which is a wrapper for Webpack. You can use it to easily integrate Vue.js into your Laravel project. By default, Laravel includes Vue.js and Axios. Ensure your package.json file has these dependencies:

      json
      "dependencies": { "axios": "^0.21", "bootstrap": "^4.0.0", "cross-env": "^5.1", "jquery": "^3.2", "laravel-mix": "^6.0.6", "lodash": "^4.17.19", "popper.js": "^1.12", "vue": "^2.5.7" }
    • Install the dependencies:

      bash
      npm install
  3. Create Vue Components:

    • Create Vue components in the resources/js/components directory.
  4. Compile Assets:

    • Laravel Mix will compile your assets and place them in the public directory. Run:

      bash
      npm run dev

    This command will run the Webpack Mix to compile your assets.

  5. Setup Routes:

    • Create routes in your Laravel application to render your Vue components. For example, in your web.php file:

      php
      use Illuminate\Support\Facades\Route; Route::get('/', function () { return view('welcome'); }); Route::get('/vue', function () { return view('vue'); });

    Here, the 'vue' route would correspond to a Blade view that includes your Vue component.

  6. Create Blade Views:

    • Create Blade views (e.g., resources/views/vue.blade.php) to render your Vue components:

      html
      <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>Laravel Vue</title> <link rel="stylesheet" href="{{ mix('css/app.css') }}"> </head> <body> <div id="app"> <!-- Your Vue component goes here --> <example-component></example-component> </div> <script src="{{ mix('js/app.js') }}"></script> </body> </html>
  7. Run Laravel Development Server:

    • Start the Laravel development server:

      bash
      php artisan serve

    This will start the server at http://localhost:8000 by default.

  8. Access Your Application:

    • Open your browser and go to http://localhost:8000/vue (or the route you defined) to see your Laravel application with Vue.js components.

Remember to update your routes, views, and components according to your project's requirements. This is a basic setup, and you can customize it further based on your specific needs.