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