laravel vue global function

To create a global function in a Vue.js application using Laravel, you can follow these steps:

  1. Open your Laravel project in your preferred code editor.

  2. Navigate to the resources/js directory and create a new file for your global function. You can name it whatever you prefer, for example, globalFunctions.js.

  3. In the globalFunctions.js file, define your global function. For example, let's say you want to create a function called formatDate that formats a given date string:

export function formatDate(date) {
  // Your code to format the date
  // ...
  1. Save the globalFunctions.js file.

  2. Open the resources/js/app.js file.

  3. Import the globalFunctions.js file at the top of the app.js file:

import './globalFunctions';
  1. Save the app.js file.

  2. Compile your JavaScript assets using Laravel Mix. You can do this by running the following command in your terminal:

npm run dev


yarn dev
  1. Once the assets are compiled, you can now use your global function throughout your Vue components.

For example, if you have a Vue component called MyComponent.vue, you can use the formatDate function like this:

    <p>{{ formattedDate }}</p>

export default {
  data() {
    return {
      date: '2023-12-05'
  computed: {
    formattedDate() {
      return formatDate(;

In the above example, the formatDate function is being used in the computed property formattedDate to format the date data property.

That's it! You have now successfully created a global function in your Laravel Vue.js application. You can repeat these steps to add more global functions as needed.