【发布时间】:2017-10-12 11:40:49
【问题描述】:
我正在使用 laravel 5.4。 我想获取过去 7 天、15 天、30 天和 45 天创建的产品。
我在 ajax 的帮助下得到了数据。当我从 ajax 获取数据时,我想向控制器添加 where 条件。我该怎么做。
我的ajax代码:
<script>
$("input[type='checkbox']").click(function() {
var days = $(this).val();
$.ajax({
type: "get",
url: '/getcreatedat',
data: { 'days': days },
success: function(data) {
console.log(data);
},
});
});
</script>
我想通过添加条件在这里传递数据: 控制器代码:
public function category($name, $main){
$category_id = Category::where('name', $name)->value('id');
if($category_id == "")
{
$all_categories_id = Category::pluck('id');
}
else
{
$all_categories_id = Category::where('parent_id', $category_id)->pluck('id');
$all_categories_id->push($category_id);
}
$product_id = Product::where('name', 'like','%'.$main.'%')->pluck('id');
$id = ProductCategory::whereIn('product_id', $product_id)->whereIn('category_id', $all_categories_id)->pluck('id');
$products = Product::find($id);
//Categories Name in Sidebar
$category = ProductCategory::whereIn('product_id', $id)->pluck('parent_id');
$category_name = Category::whereIn('id', $category)->pluck('name');
//Categories Name in dropdown
$main_categories = Category::where('parent_id', '0')->pluck('name');
//Product Image Mapping
$products->map(function ($product) {
$directory = 'uploads/products/images/'.$product->id;
$brand = Brand::select('name')->where('id', '=', $product->brand)->pluck('name');
$brand_name = $brand->first(function($value, $key) {
return $key == 'name';
});
if (is_dir($directory)) {
$files = scandir ($directory);
$img_file = $directory.'/'.$files[2];
$product['front_img'] = $img_file;
$product['brand'] = $brand_name;
return $product;
}
return $product;
});
return view('pages/product', compact('main_categories', 'products', 'name', 'category_name'));
}
【问题讨论】:
标签: jquery ajax laravel laravel-5.4