Answer the question
In order to leave comments, you need to log in
Why is a layered json object returned (ajax+laravel+underscore)?
The situation is the following. I have a product filter on my site. The structure is: Collection -> Product. The collection stores the type, formats, manufacturer, and in the product, in addition to everything else, the price and name of the collection. Accordingly, when filtering prices, we first look for a product with the corresponding price, pull out the name of the collection from the product, and then search for the collection by this name and show this collection in the form. The problem is this:
here is the laravel controller code paste.jesse-obrien.ca/in
and here is the multi-level json object that returns the controller https://www.filepicker.io/api/file/xQRk9rSKSKW0HSFvKyLC . I use the underscore template engine, naturally if the object is multi-level, then access to the attributes in the template engine becomes impossible. Don't ask why the get request. So I need it so easy. Here is the ajax request
if(type != "0") {
$.ajax({
type: 'get',
url: '/filter/type/'+type+'/min/' + min + '/max/'+max,
dataType: 'json',
success: function(data) {
$(".prodlist").html(_.template(template,{data:data }));
}
});
}
<%
// repeat items
_.each(data,function(item,key,list){
_.each(data,function(item,key,list){
%>
<li id="item134" data-id="134">
<div class="cube cube1">
<div class="cfront">
<div class="img">
<a href="/collection/<%= item.attributes.title %>">
<img src="/images/collection/<%= item.attributes.preview_img %>" class="default">
</a>
</div>
</div>
<div class="cube cube2">
<div class="cfront">
<h3>Коллекция <a href="/collection/<%= item.attributes.title %>"><%= item.attributes.title %></a></h3>
<p>ФОРМАТЫ:
<% _.each(products,function(products,key,list){ %>
<span><%= products.formats %></span>
<% }); %>
<ul class="items4">
<li style="width:100%;text-align:left;padding-left:6%">
ПРОИЗВОДИТЕЛЬ:
<% _.each(manufacturers,function(manufacturer,key,list){ %>
<% if(manufacturer.attributes.id == item.attributes.manufacturers_id) { %>
<%= manufacturer.attributes.title %>,
<% } %>
<% }); %>
<% _.each(country,function(c,key,list){ %>
<% if(c.attributes.id == item.attributes.country_id) { %>
<%= c.attributes.title %>
<% } %>
<% }); %>
</li>
</ul>
</div>
</div>
<div class="cube cube3">
<div class="cfront">
<div>
<div class="resolution">
<strong>
</strong> <span> <em>руб.</em></span>
<strong>73</strong> <span>эелементов <em>в коллекции</em></em></span>
<a href="/collection/<%= item.attributes.title %>"><span><span></span></span></a>
</div>
</div>
</div>
</div>
</li>
<%
}); });
%>
Answer the question
In order to leave comments, you need to log in
If I understand the problem correctly, then: in the fifth line of the controller, you add the result of the query execution to the array, and this is an array of objects, not a separate object, even if the query returns only one row or does not return anything at all.
Those. i would replace
on the
In general, learn how to do SQL JOIN in your ORM. The task is solved with one request.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question