Filtering data in Laravel can be efficiently achieved by utilizing Laravel's ORM hasmany feature in conjunction with Vue

Hey there, I'm currently working with Laravel ORM and Vue 2.

I've encountered some issues with analyzing Json data.

Here's my Laravel ORM code:

$banner = Banner::with('banner_img')->get();
return response()->json($banner);

This is the Json data I am dealing with:

[{"id":10,"banner":"AIR","banner_img":[{"id":1,"img":"air_1.png","banner_id":10},{"id":2,"img":"air_2.png","banner_id":10}]},
{"id":11,"banner":"HOT","banner_img":[{"id":3,"img":"hot_1.png","banner_id":11},{"id":4,"img":"hot_2.png","banner_id":11}]},
{"id":12,"banner":"NEW","banner_img":[{"id":5,"img":"new_1.png","banner_id":12},{"id":6,"img":"new_2.png","banner_id":12}]}]

My json data contains two arrays.

I need to filter this json data (banner_img:['img']) using Vue.js.

For Vue.js, here's what I have:

var app = new Vue({
el: '#app',
data: {
    banner:[],
    search:'',
},
methods: {
    getBannerData: function() {
        axios.get('/case/ajax/33').then(response => {
            this.banner = response.data.banner;
        });
    },
},
mounted: function() {
   this.getBannerData();
},
computed: {
    filteredList() {
      return this.banner(value => {
        return value.banner.banner_img.img.toLowerCase().includes(this.search.toLowerCase())
      })
    }
}
});

As for the HTML part:

<input type="text" name="ImgFilter" v-model="search">
<div v-for="value in filteredList">
    <img v-for="imgs in value.banner_img" :src="imgs.img" height="100">
</div>

I tried

return value.banner.banner_img.img
, but that didn't work as expected.

Any advice or suggestion would be greatly appreciated. Thanks in advance!

Answer №1

After analyzing the JSON sample you provided, it is evident that each object contains an array of values under the banner_img property. Therefore, the statement value.banner.banner_img.img is inaccurate because the value.banner does not possess the banner_img property.

Consider revising your code to this:

filteredList() {
    return this.banner.filter(value => {
        return value.banner_img.filter(bannerImg => {
            return bannerImg.img.toLowerCase().includes(this.search.toLowerCase());
        });
    });
}

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

Tips for determining the height of the entire webpage using jQuery

I attempted the following code: $(window).height(); $("#content").height(); However, it did not provide an accurate value. ...

Notification access is consistently denied

In my coding work, I rely on Notification.permission to determine if the browser supports notifications or not. Here's a snippet of how I handle Notification.permission in my script: // Verify browser notification support if (!("Notification" in windo ...

Updating your Heroku app (Node.js) from a GitHub repository - a step-by-step guide

I attempted to deploy my React app using the following process: git status git remote add origin <repo link> git commit -m "node js" git add . Unfortunately, this method did not work for me. Can anyone provide guidance on how to update a ...

Exclude the initial and final lines from the document in JSON format

Is there a way to have json_decode ignore the first and last lines of a JSON file in order to produce valid JSON output? The contents of my file are as follows: while(true);/* 0; JSON CODE /* */ This is the PHP code I am using: $json = file_get_content ...

Guide on calculating the quantity of rows in a Json data set

How can I determine the number of rows where my ID is present in one of the fields, which are stored as JSON objects: { "Monday":{"1":"15","2":"27","3":"74","4":"47","5":"42","6":"53"}, "Tuesday":{"1":"11","2":"28","3":"68","4":"48","5":"43","6":"82"} ...

Tips for creating a scrollable smiley input field

I have a chat system where I am incorporating smileys from a predefined list. QUERY I am looking to create a scrolling feature for the smileys similar to how it is implemented on this particular website. The twist I want to add is to have this functiona ...

"Utilizing datatables to efficiently submit form data to the server-side

I'm curious for those who utilize the Datatables javascript plugin, is there a way to replicate this specific example using server-side data? The current example I came across has its data hardcoded directly in the HTML. ...

Tips for fading the text of list items when their checkbox is marked as done?

I am trying to figure out how to gray out a list item when its checkbox is checked. The code I currently have takes text input from a textbox and adds it to an unordered list when the add button is clicked. Each list item contains a checkbox within it. My ...

Error when compiling TypeScript: The callback function provided in Array.map is not callable

This is a Node.js API that has been written in Typescript. app.post('/photos/upload', upload.array('photos', 12), async (req, res) => { var response = { } var list = [] try { const col = await loadCollection(COLLECTION_NAM ...

Incorporating react-intl-tel-input into your project: A step

Greetings, I am currently delving into the realm of ReactJS and am tasked with incorporating react-intl-tel-input to capture phone numbers from around the globe. However, during the integration process, I encountered some obstacles. When I input this code: ...

Ember and Mongoose are not providing the expected record returns

Currently facing an issue with Ember and models. My goal is to return JSON from a Mongoose API using Express. The problem lies in the fact that the route successfully returns the JSON, but upon trying to retrieve it on the front end (Ember), it appears as ...

How can I remove the focus highlight from the MUI DatePicker while retaining it for the TextField?

I am currently working on developing a date picker using react.js with the MUI DatePicker library. The code I have implemented closely resembles what is provided in their documentation. However, upon rendering the component, I am encountering an issue whe ...

Updating switch data on click using Vuejs: A step-by-step guide

Could someone please explain to me how to swap two different sets of data when clicked? For instance, let's say we have the following data: data() { return { data_one: [ { name: "Simo", ...

Guide to assigning an integer value to a string in Angular

Is there a way to change integer values in a table to actual names and display them on a webpage by setting a scope in the controller and passing that string to the HTML? For example, this is an example of the HTML code for the table row: <thead> ...

unable to change Json string into a JSONObject or JSONArray

I have developed a REST WCF web service and hosted it on my local IIS. To convert the JSON string, I am using JsonConvert.SerializeObject(object) from the Newtonsoft package. The output of the web service is: "[{\"companyId\":2,\"companyNa ...

What is the best way to format a date as "2020-01-16 07:29:43.657519000 Z" using JavaScript?

Upon discovering a format 2020-01-16 07:29:43.657519000 Z in a project, I am curious about the significance of the numbers 657519000 and how to replicate this precise format using JavaScript. When attempting new Date().toISOString(), I receive 2020-05-27T2 ...

Variables are losing their way in the vast expanse of self-submitting

I have a form that needs to be submitted on the same page (index.php) and I want to capture the entered information as a JavaScript variable. <?php $loginid = $_POST[username] . $_POST[password]; ?> Here is some basic code: <script> var pass ...

jQuery parallax effect enhances scrolling experience with background images

My website features a parallax design, with beautiful high-resolution images in the background that dominate the page. Upon loading the site, the landing page showcases a striking, large background image alongside a small navigation table ('about&apos ...

Tips on showing a specific div when a button is clicked using jQuery and Bootstrap

In my button group, I have four buttons and I need one specific button to display certain div content when clicked. The displayed content should be based on which button is clicked. Is there a way to achieve this using Twitter Bootstrap jQuery in ASP.NET ...

JavaScript - All values stored from the for loop are registering as undefined

Recently delving into the realm of JavaScript programming, I find myself faced with a new challenge. While not my first language, this is one of my initial ventures with it. My current project involves creating a chess program utilizing the HTML5 canvas fe ...