Commit efcb1ff2 authored by TTS Tran Viet Anh's avatar TTS Tran Viet Anh

using scope

parent a8178db3
......@@ -16,16 +16,18 @@ class HomeController extends Controller
*/
public function index()
{
$productCategories = ProductCategory::query()->where('parent_id', '=', null)->get();
$latestDrops = Product::query()
->with('productAttributes')
$productCategories = ProductCategory::noParent()->get();
$latestDrops = Product::withAttributes()
->statusEnabled()
->latest()
->take(4)
->get();
$products = Product::query()->latest()->take(8)->get();
$latestBlogs = Post::query()
->with('user')
$products = Product::withAttributes()
->statusEnabled()
->latest()
->take(8)
->get();
$latestBlogs = Post::withUser()
->statusPublished()
->latest()
->take(3)
......@@ -35,13 +37,12 @@ public function index()
public function getProductsByCategory(Request $request)
{
$cats = ProductCategory::query()
->where('id', '=', $request->cat)
->orWhere('parent_id', '=', $request->cat)
$cat = $request->cat;
$subCats = ProductCategory::where('parent_id', $cat)
->get('id')
->toArray();
$productsByCategory = Product::query()
->whereIn('product_category_id', $cats)
array_push($subCats, $cat);
$productsByCategory = Product::whereIn('product_category_id', $subCats)
->latest()
->take(8)
->get();
......
......@@ -29,6 +29,11 @@ public function user()
public function scopeStatusPublished($query)
{
return $query->where('status', '=', Post::STATUS_PUBLISHED);
return $query->where('status', Post::STATUS_PUBLISHED);
}
public function scopeWithUser($query)
{
return $query->with('user');
}
}
......@@ -30,4 +30,9 @@ public function children()
{
return $this->belongsTo(PostCategory::class, 'parent_id');
}
public function scopeNoParent($query)
{
return $query->where('parent_id', null);
}
}
......@@ -28,6 +28,11 @@ public function productAttributes()
public function scopeStatusEnabled($query)
{
return $query->where('status', '=', Product::STATUS_ENABLED);
return $query->where('status', Product::STATUS_ENABLED);
}
public function scopeWithAttributes($query)
{
return $query->with('productAttributes');
}
}
......@@ -30,4 +30,9 @@ public function children()
{
return $this->belongsTo(ProductCategory::class, 'parent_id');
}
public function scopeNoParent($query)
{
return $query->where('parent_id', null);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment