Provide property to enduring design

I need help with sending the 'products' prop to my persistent layout in order for it to display the products in the search field and filter them correctly. How can I achieve this? Thank you!

app.js


        ...
        import MainLayout from "./Pages/Layouts/MainLayout.vue";

        createInertiaApp({
            title: (title) => `${title} - ${appName}`,
            resolve: (name) => {
                const pages = import.meta.glob("./Pages/**/*.vue", { eager: true });
                let page = pages[`./Pages/${name}.vue`];

                page.default.layout = name.startsWith("Dashboard/")
                    ? undefined
                    : MainLayout;
                return page;
            },

            setup({ el, App, props, plugin }) {
                return createApp({ render: () => h(App, props) })
                   ...
                   .mount(el);
            },
            progress: {
                color: "#4B5563",
            },
        });

    

Answer №1

To resolve the issue, I created a custom route that serves products in JSON format:

Route::get('/products',function(){
    $products = DB::table('products')->get();

    return response()->json([
        'products' => $products,
    ]);
});

Then, I accessed this route as an API using fetch/axios and watchEffect:

import { watchEffect, ref } from "vue";

const products = ref(null);

watchEffect(async () => {
    const response = await fetch(`/products`);
    products.value = await response.json();
    console.log(products.value);
});

UPDATE

You don't actually need to utilize watchEffect in this scenario since there are no reactive dependencies within it. You can simply use fetch at the root of the script.

I recommend refraining from using refs for non-primitive data types to avoid unwrapping them in the script section.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Capacitor: Building without push-notifications plugin is not possible

I have been attempting to utilize the capacitor push notifications plugin in a quasar project. I followed this tutorial as a guide. However, upon running the command: quasar build -m capacitor -T android, I encountered this error within my quasar project: ...

Experiencing excessive CPU utilization from Node.js while executing the vue-cli-service serve command

Whenever I run my npm script that uses vue-cli-service serve, the CPU usage by Node exceeds 100%. How can I troubleshoot this problem? I am using a Mac and have installed Node through nvm. My Node version is 10.16 and my npm version is 6.9. ...

Adding local static files to index.html in Vue 3 can be achieved by utilizing the

<!DOCTYPE html> <html lang=""> <head> <link rel="icon" href="<%= BASE_URL %>favicon.ico"> <title><%= htmlWebpackPlugin.options.title %></title> <script src=" ...

Having trouble creating a scatter chart using vue-charts library

Whenever I try to render the chart, all I get is a blank page along with this error message: TypeError: Cannot read property 'getBasePixel' of undefined There seems to be an issue with my implementation in Vue.js even though I followed the ex ...

Laravel - Backpack linking select boxes based on another select box

After integrating the Laravel Backpack admin panel into my Laravel project, I am currently in the process of customizing it. However, I have encountered some challenges and require assistance. My Objective: I aim to implement two select elements: one for ...

Guide for setting up Laravel Homestead using VirtualBox.box installation

Click here for step-by-step instructions on setting up Homestead for Laravel 5.2 After following the documentation provided above, I encountered an issue when running the command vagrant box add laravel/homestead. To resolve this, I attempted to manually ...

"Getters appear to be malfunctioning and it seems that the state value remains immutable within the Vuex

After diving into VueX for the first time, I encountered some challenges: Struggling to access state using getters in store.js. While this.$store.state.java works fine, switching to this.$store.getters.semuaJasa results in no display on the browser. Unabl ...

Is it considered acceptable to utilize a v-model's value as the basis for an if-statement?

How can I incorporate the v-model="item.checked" as a condition within the validations() function below? <table> <tr v-for="(item, i) of $v.timesheet.items.$each.$iter" > <div> <td> ...

"Is it possible to implement lazy-loading in Vue3 for one view from another

Is there a way to dynamically load a view from another view? For instance, if the user is on /auth, can we lazy-load the Fillout component while they fill out some inputs and then navigate to Fillout once they are done? I couldn't find any informatio ...

How do I retype an interface from a dependency?

It's difficult to put into words, so I have created a repository for reference: https://github.com/galenyuan/how-to-retyping My goal is to achieve the following: import Vue from 'vue' declare module 'vuex/types/vue' { interfac ...

Storing data from an API response into the localStorage using Vue.js upon clicking

My objective is to save specific data in the browser's localStorage upon clicking a link. However, the output I receive is either undefined or completely empty. <li v-for="(category, index) in categories" :key="index"> ...

Tips for obtaining consistent legend and tooltip for dynamically generated data in Vue using vue-chartjs

I am currently working on creating a vue chart using vuechartjs that would display the numbers 10, 20, 30, 40, 50 repeatedly over several years. My main issue is getting the legend and tooltip to function correctly. At the moment, the label just shows an ...

A Vue filtering feature that consistently adds 5 additional elements upon each use

I was wondering, how can I create a computed property filter function that always adds 5 more elements to the current array? Here are more details: Template: <span class="box-content" v-for="item in activeItems" :key="item.id"> <img class=" ...

Is there a way to pre-fill meta tags in Nuxt before the page finishes loading?

Having a Nuxt3 application, I encounter an issue where a blog article fetched from Firestore using a path variable ID is not populating the title and meta tags (<Title> and <Meta>) in time for SEO optimization. Despite setting SSR to true and p ...

Guide to fetching and displaying a PDF file in the browser using Vue.js and Axios

Despite successfully using Axios to download a pdf file on the browser with correct page numbers and orientation, the content appears to be empty. Here is the API code snippet: [HttpGet] [Route("~/api/Document")] public HttpResponseMessage Get(in ...

Solving the CORS problem between Vue.js and Flask: Troubleshooting XMLHttpRequest Blockade

Description: Currently working on developing a web application utilizing Vue.js for the frontend and Flask for the backend. The initial phase involves creating a simple login page, but encountering CORS (Cross-Origin Resource Sharing) issues when making r ...

Update the Vue component upon fetching new data

How can I continuously refresh the list of items when a button in a sibling component is clicked? The watch method only triggers once, but I need it to constantly refresh. This is the parent element: <template> <div class="container"& ...

Upon mounting, Vue.js 3 composable data is not available

Currently, I am utilizing Vue.js 3 along with a basic test composable: TEST COMPOSABLES Load post id: {{ i }} <div v-if="error"> <p>Uh oh! An error has occurred: {{ error.message }}</p> <button @click="r ...

I am having trouble getting PHP to parse JSON in the way I need

Currently, I am working on a hobby project that involves an API for accessing data. While everything seems to be functioning well on the other ends, I am facing an issue with the JSON array format. I need the JSON array to not have brackets so that I can e ...

Using AXIOS and VueJs to visually represent data with a two-dimensional array

I'm new to vuejs and I want to display my data response in a two-dimensional table. This is the data I have: [{ "origine": "", "nb": 15 }, { "origine": "?", "nb": 18 }, { "origine": "?L", "nb": 1 }, { "origine": "G", "nb": 298 }, { ...