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

fix code and using accessor

parent efcb1ff2
...@@ -17,18 +17,18 @@ class HomeController extends Controller ...@@ -17,18 +17,18 @@ class HomeController extends Controller
public function index() public function index()
{ {
$productCategories = ProductCategory::noParent()->get(); $productCategories = ProductCategory::noParent()->get();
$latestDrops = Product::withAttributes() $latestDrops = Product::with('productAttributes')
->statusEnabled() ->enabled()
->latest() ->latest()
->take(4) ->take(4)
->get(); ->get();
$products = Product::withAttributes() $products = Product::with('productAttributes')
->statusEnabled() ->enabled()
->latest() ->latest()
->take(8) ->take(8)
->get(); ->get();
$latestBlogs = Post::withUser() $latestBlogs = Post::with('user')
->statusPublished() ->published()
->latest() ->latest()
->take(3) ->take(3)
->get(); ->get();
...@@ -37,12 +37,8 @@ public function index() ...@@ -37,12 +37,8 @@ public function index()
public function getProductsByCategory(Request $request) public function getProductsByCategory(Request $request)
{ {
$cat = $request->cat; $category = ProductCategory::findOrFail($request->cat);
$subCats = ProductCategory::where('parent_id', $cat) $productsByCategory = Product::ofCategories($category->ids)
->get('id')
->toArray();
array_push($subCats, $cat);
$productsByCategory = Product::whereIn('product_category_id', $subCats)
->latest() ->latest()
->take(8) ->take(8)
->get(); ->get();
......
...@@ -27,13 +27,8 @@ public function user() ...@@ -27,13 +27,8 @@ public function user()
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
public function scopeStatusPublished($query) public function scopePublished($query)
{ {
return $query->where('status', Post::STATUS_PUBLISHED); return $query->where('status', Post::STATUS_PUBLISHED);
} }
public function scopeWithUser($query)
{
return $query->with('user');
}
} }
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
class PostCategory extends Model class PostCategory extends Model
{ {
const STATUS_ENABLED = 1; const STATUS_ENABLED = 1;
const STATUS_DISABLE = 2; const STATUS_DISABLED = 2;
const STATUS_DELETED = 3; const STATUS_DELETED = 3;
use HasFactory, SoftDeletes; use HasFactory, SoftDeletes;
...@@ -28,11 +28,11 @@ public function parent() ...@@ -28,11 +28,11 @@ public function parent()
public function children() public function children()
{ {
return $this->belongsTo(PostCategory::class, 'parent_id'); return $this->hasMany(PostCategory::class, 'parent_id');
} }
public function scopeNoParent($query) public function scopeNoParent($query)
{ {
return $query->where('parent_id', null); return $query->whereNull('parent_id');
} }
} }
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
class Product extends Model class Product extends Model
{ {
const STATUS_ENABLED = 1; const STATUS_ENABLED = 1;
const STATUS_DISABLE = 2; const STATUS_DISABLED = 2;
const STATUS_DELETED = 3; const STATUS_DELETED = 3;
use HasFactory, SoftDeletes; use HasFactory, SoftDeletes;
...@@ -26,13 +26,13 @@ public function productAttributes() ...@@ -26,13 +26,13 @@ public function productAttributes()
return $this->hasMany(ProductAttribute::class); return $this->hasMany(ProductAttribute::class);
} }
public function scopeStatusEnabled($query) public function scopeEnabled($query)
{ {
return $query->where('status', Product::STATUS_ENABLED); return $query->where('status', Product::STATUS_ENABLED);
} }
public function scopeWithAttributes($query) public function scopeOfCategories($query, $cat_ids)
{ {
return $query->with('productAttributes'); return $query->whereIn('product_category_id', $cat_ids);
} }
} }
...@@ -28,11 +28,16 @@ public function parent() ...@@ -28,11 +28,16 @@ public function parent()
public function children() public function children()
{ {
return $this->belongsTo(ProductCategory::class, 'parent_id'); return $this->hasMany(ProductCategory::class, 'parent_id');
}
public function getIdsAttribute()
{
return collect($this->children->pluck('id'))->prepend($this->id)->all();
} }
public function scopeNoParent($query) public function scopeNoParent($query)
{ {
return $query->where('parent_id', null); return $query->whereNull('parent_id');
} }
} }
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