gpt4 book ai didi

javascript - 在 Laravel 中获取基于 Ajax 的数据

转载 作者:行者123 更新时间:2023-11-28 03:44:22 24 4
gpt4 key购买 nike

我尝试创建一个查找器(一种高级搜索),但我已经完成了一半,但在获取搜索查询的第三部分时遇到了问题。

逻辑

  1. 选择类别
  2. 选择子类别
  3. 选择子类别中的产品规范
  4. 选择子类别中的品牌

screen1

基本上用户会选择类别,然后选择该类别的子类别直到这里我已经完成然后该子类别产品的所有规范和品牌都会呈现出来供选择。

问题

My problem is to get specifications and brands from selected subcategory.

我想要根据所选子类别获取这些信息的原因是因为每个子类别产品都有不同的品牌和规范,例如我不想在用户选择鼠标时显示cpu规范 子类别!任何类型的鼠标都没有 CPU,这就是全部原因。

代码

Blade

<form action="" class="mt-20">
<div class="col-md-3">
<label for="category_id">category</label>
<select name="category_id" class="form-control">
<option value="">Select Category</option>
@foreach($findercategories as $category)
<option value="{{$category->id}}">{{$category->title}}</option>
@endforeach
</select>
</div><!-- end col-md-3 -->
<div class="col-md-3">
<label for="subcategory_id">subcategory</label>
<select name="subcategory_id" class="form-control">
<option value="">Select Subcategory</option>
</select>
</div><!-- end col-md-3 -->
<div class="col-md-3">
<label for="specification_id">specifications</label>
<select name="specification_id" class="form-control">
<option value="">Select Specification</option>
</select>
</div><!-- end col-md-3 -->
<div class="col-md-3">
<label for="brand_id">brands</label>
<select name="brand_id" class="form-control">
<option value="">Select Brand</option>
</select>
</div><!-- end col-md-3 -->
<div class="row">
<div class="col-md-3 col-md-offset-9">
<button style="margin: 20px;" class="pull-right btn btn-info" type="submit">Find</button>
</div>
</div>
</form>

Javascript

<script type="text/javascript">
$(document).ready(function() {
$('select[name="category_id"]').on('change', function() {
var categoryID = $(this).val();
if(categoryID) {
$.ajax({
url: '{{ url('getSubCategories') }}/'+encodeURI(categoryID),
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="subcategory_id"]').empty();
$.each(data, function(key, value) {
$('select[name="subcategory_id"]').append('<option class="form-control" value="'+ value['id'] +'">'+ value['title'] +'</option>');
});
}
});
}else{
$('select[name="subcategory_id"]').empty();
}
});
});
</script>

<script type="text/javascript">
$(document).ready(function() {
$('select[name="subcategory_id"]').on('change', function() {
var subcategoryID = $(this).val();
if(subcategoryID) {
$.ajax({
url: '{{ url('getspecifications') }}/'+encodeURI(subcategoryID),
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="specification_id"]').empty();
$.each(data, function(key, value) {
$('select[name="specification_id"]').append('<option class="form-control" value="'+ value['id'] +'">'+ value['title'] +'</option>');
});
}
});
}else{
$('select[name="specification_id"]').empty();
}
});
});
</script>

Controller

//get categories list
$findercategories = Category::ofStatus('Active')->get();

//finder (subcategories)
public function getSubCategories($id){
$subcategory = Subcategory::where('category_id', $id)->get();
return response()->json($subcategory);
}

//finder (specifications + brands)
public function getspecifications($id){
$product = Product::where('subcategory_id', $id)->get();
$productsubsp = DB::table('product_subspecification')->where('product_id', $product)->get();
$specifications = $productsubsp;
return response()->json($specifications);
}

路线

Route::get('/getSubCategories/{id}', 'frontend\SearchController@getSubCategories');
Route::get('/getspecifications/{id}', 'frontend\SearchController@getspecifications');

额外(只是为了清楚我尝试获取的数据)

  1. 我的产品品牌正在使用 ID 保存在 products 表的列中名为brand_id
  2. 我的产品规范保存在名为product_subspecation 将获取 id product_idsubspecification_id

这就是我在前端获取这些信息的方式:

针对品牌

{{$product->brand->title}}

以及规范

在 Controller 中

$specifications = $product->subspecifications->mapToGroups(function ($item, $key) {
return [$item->specification->title => $item];
});

在 Blade 中

@foreach($specifications as $specificationtitle => $specificationcollection)
<tr>
<th style="width:150px;">{{ $specificationtitle }}</th>
<td class="text-left">
@foreach($specificationcollection as $subspecification)
{{$subspecification->title}}
@endforeach
</td>
</tr>
@endforeach

返回结果如下:

CPU CORE I5 CORE I3 CORE I7

RAM 2 GIG 4 GIG

更新

我设法在规范下拉列表中获得结果,但是我无法在所选子类别下获取所有产品我只能获取最后一个产品,而不是全部。

代码

这是我当前获取结果的方式

public function getspecifications($id){
//find products under subcategory
$product = DB::table('products')
->where('subcategory_id', $id)
->get();
foreach($product as $pp) {
}
$specificationsw = $pp->id; //get products id (currently has issue, only gets 1)

// find products specification with same product id
$specificationss = DB::table('product_subspecification')
->where('product_id', $specificationsw)->get();
foreach($specificationss as $dd) {
}
$specificationsss = $dd->subspecification_id; //get specifications id from product_subspecification table


// get specifications title base on table above
$specifications = DB::table('subspecifications')
->where('id', $specificationsss)->get();
return response()->json($specifications);
}

知道如何获取我的所有产品id,而不是仅获取我当前获取的最后一个产品吗?

更新2

我将 Controller 更改为下面的代码,现在我可以从子类别中获取所有产品:

public function getspecifications($id){
//find products under subcategory
$product = DB::table('products')
->where('subcategory_id', $id)
->get();
$date = array();
foreach ($product as $stan) {
$date[] = $stan->id;
}
$specificationsw = $date; //get products id (currently has issue, only gets 1)

// find products specification with same product id
$specificationss = DB::table('product_subspecification')
->where('product_id', $specificationsw)->get();
$date2 = array();
foreach ($specificationss as $stan2) {
$date2[] = $stan2->subspecification_id;
}
$specificationsss = $date2; //get specifications id from product_subspecification table

// get specifications title base on table above
$specifications = DB::table('subspecifications')
->where('id', $specificationsss)->get();
$date3 = array();
foreach ($specifications as $stan3) {
$date3[] = $stan3;
}
$specifications = $date3;
return response()->json($specifications);
}

现在这是我的 dd 结果:

dd($specationssw); products dd 这是产品的 id 2 个产品

array:2 [▼
0 => 27
1 => 38
]

dd($specationsss);product_subspecification 表结果仅返回产品 id 27 的值。产品 id 38 不返回值!

array:2 [▼
0 => 5
1 => 8
]

dd($specations); 我的函数最后一部分的结果我在前端的ajax代码中得到了这个标题但是正如你看到的关于的信息>subspesification id 8product id 38 丢失。

array:1 [▼
0 => {#722 ▼
+"id": 5
+"title": "4 GIG"
+"specification_id": 2
+"created_at": "2017-12-04 00:00:00"
+"updated_at": "2017-12-04 00:00:00"
}
]

最佳答案

已解决

我在这里解决了我的问题,供有需要的人使用:

说明:

  1. Controller 的第一个方法将根据选择获取子类别类别 ID。
  2. 第二种方法将根据选定的子类别显示规范注释在我的案例中,规范存储在其表中并与我的产品相关 sync method在第三个表中。 (仅显示该子类别下的产品中使用的规范)
  3. 第三种方法将根据所选子类别显示品牌(仅显示该子类别下的产品中使用的品牌)

Controller 方法

//finder (find subcategories base on selected category)
public function getSubCategories($id){
$subcategory = Subcategory::where('category_id', $id)->get();
return response()->json($subcategory);
}

//finder (show specifications base on selected subcategory)
public function getspecifications($id){
$specifications = DB::table('products')
->where('subcategory_id', $id)
->join('product_subspecification', 'product_subspecification.product_id', '=', 'products.id')
->join('subspecifications', 'subspecifications.id', '=', 'product_subspecification.subspecification_id')
->groupBy('subspecifications.id')
->get();
return response()->json($specifications);
}

//finder (show brands base on selected subcategory)
public function getbrands($id){
$specifications = DB::table('products')
->where('subcategory_id', $id)
->join('brands', 'brands.id', '=', 'products.brand_id')
->groupBy('brand_id')
->get();
return response()->json($specifications);
}

Important: In order to avoid duplicated results in specifications and brands I used groupBy.

路线

Route::get('/getSubCategories/{id}', 'frontend\SearchController@getSubCategories');
Route::get('/getspecifications/{id}', 'frontend\SearchController@getspecifications');
Route::get('/getbrands/{id}', 'frontend\SearchController@getbrands');

脚本

<script type="text/javascript">
$(document).ready(function() {
$('select[name="category_id"]').on('change', function() {
var categoryID = $(this).val();
if(categoryID) {
$.ajax({
url: '{{ url('getSubCategories') }}/'+encodeURI(categoryID),
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="subcategory_id"]').empty();
$.each(data, function(key, value) {
$('select[name="subcategory_id"]').append('<option class="form-control" value="'+ value['id'] +'">'+ value['title'] +'</option>');
});
}
});
}else{
$('select[name="subcategory_id"]').empty();
}
});
});
</script>

<script type="text/javascript">
$(document).ready(function() {
$('select[name="subcategory_id"]').on('change', function() {
var subcategoryID = $(this).val();
if(subcategoryID) {
$.ajax({
url: '{{ url('getspecifications') }}/'+encodeURI(subcategoryID),
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="specification_id"]').empty();
$.each(data, function(key, value) {
$('select[name="specification_id"]').append(
"<option class='form-control' value='"+ value['id'] +"'>"+ value['title'] +"</option>"
);
});
}
});
}else{
$('select[name="specification_id"]').empty();
}
});
});
</script>

<script type="text/javascript">
$(document).ready(function() {
$('select[name="subcategory_id"]').on('change', function() {
var subcategoryID = $(this).val();
if(subcategoryID) {
$.ajax({
url: '{{ url('getbrands') }}/'+encodeURI(subcategoryID),
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="brand_id"]').empty();
$.each(data, function(key, value) {
$('select[name="brand_id"]').append('<option class="form-control" value="'+ value['id'] +'">'+ value['title'] +'</option>');
});
}
});
}else{
$('select[name="brand_id"]').empty();
}
});
});
</script>

关于javascript - 在 Laravel 中获取基于 Ajax 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48638054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com