<?php
namespace App\Http\Controllers;
use DB;
class CategoryController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
$this->Categories = DB::table('categories');
$this->Highlight = DB::table('category_highlights');
$this->CategoryIndex = DB::table('category_indices');
$this->Products = DB::table('products');
$this->Sugesstions = DB::table('sugesstions');
}
// api: /categories/getTreeCategories
public function getTreeCategory(){
$results = self::getCategory();
// $categories = self::getTree($results);
echo json_encode($results);
}
// Ham lay category
public function getCategory(){
return $this->Categories
->select('id','name','slugify','cover','parent_id')
->where('status','public')
->get();
}
// api: /categories/getCategoryHighlight
public function getCategoryHighlight(){
$results = self::getHighlight();
echo json_encode($results);
}
// Ham lay category highlight
public function getHighlight(){
return $this->Highlight
->select('id','category_id','product_id','order')
->where('status','public')
->get();
}
// api: /categories/getCategoryIndices
public function getCategoryIndices(){
$index = self::getDataCategoryIndex();
$categories = $index->map( function($item, $key){
$category = self::getCategoryById($item->category_id);
return $category;
});
$categories = $categories->reject(function($item, $k){
return $item == null;
});
$categories = $categories->map(function($data, $i){
$data->products = self::getProductOfCategoryIndex($data->id,8);
return $data;
});
echo json_encode($categories);
}
// Hàm lấy 8 sản phẩm cho category index
public function getProductOfCategoryIndex($id_category,$num){
return $this->Products
->select('id','name','slugify','price','promotion','category_id','status','created_at')
->where(['category_id' => $id_category, 'status' => 'public'])
->orderBy('created_at','asc')
->get($num);
}
// Hàm lấy category theo id category
public function getCategoryById($id){
return $this->Categories
->where([
'id' => $id,
'status' => 'public'
])
->first();
}
// Hàm lấy category trang chủ
public function getDataCategoryIndex(){
return $this->CategoryIndex
->select('id','category_id','order','status')
->where('status','public')
->get();
}
// api: /categories/getCategoryIndices
public function getSugesstions(){
$results = $this->Sugesstions->where('status','public')->get();
echo $results->toJson();
}
} //End Class