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 ...@@ -16,16 +16,18 @@ class HomeController extends Controller
*/ */
public function index() public function index()
{ {
$productCategories = ProductCategory::query()->where('parent_id', '=', null)->get(); $productCategories = ProductCategory::noParent()->get();
$latestDrops = Product::query() $latestDrops = Product::withAttributes()
->with('productAttributes')
->statusEnabled() ->statusEnabled()
->latest() ->latest()
->take(4) ->take(4)
->get(); ->get();
$products = Product::query()->latest()->take(8)->get(); $products = Product::withAttributes()
$latestBlogs = Post::query() ->statusEnabled()
->with('user') ->latest()
->take(8)
->get();
$latestBlogs = Post::withUser()
->statusPublished() ->statusPublished()
->latest() ->latest()
->take(3) ->take(3)
...@@ -35,13 +37,12 @@ public function index() ...@@ -35,13 +37,12 @@ public function index()
public function getProductsByCategory(Request $request) public function getProductsByCategory(Request $request)
{ {
$cats = ProductCategory::query() $cat = $request->cat;
->where('id', '=', $request->cat) $subCats = ProductCategory::where('parent_id', $cat)
->orWhere('parent_id', '=', $request->cat)
->get('id') ->get('id')
->toArray(); ->toArray();
$productsByCategory = Product::query() array_push($subCats, $cat);
->whereIn('product_category_id', $cats) $productsByCategory = Product::whereIn('product_category_id', $subCats)
->latest() ->latest()
->take(8) ->take(8)
->get(); ->get();
......
...@@ -29,6 +29,11 @@ public function user() ...@@ -29,6 +29,11 @@ public function user()
public function scopeStatusPublished($query) 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() ...@@ -30,4 +30,9 @@ public function children()
{ {
return $this->belongsTo(PostCategory::class, 'parent_id'); return $this->belongsTo(PostCategory::class, 'parent_id');
} }
public function scopeNoParent($query)
{
return $query->where('parent_id', null);
}
} }
...@@ -28,6 +28,11 @@ public function productAttributes() ...@@ -28,6 +28,11 @@ public function productAttributes()
public function scopeStatusEnabled($query) 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() ...@@ -30,4 +30,9 @@ public function children()
{ {
return $this->belongsTo(ProductCategory::class, 'parent_id'); 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