Answer the question
In order to leave comments, you need to log in
How to display categories and products in Laravel?
there is a Category model
class Category extends Model
{
use HasFactory;
protected $table = 'categories';
public function products()
{
return $this->hasMany(Product::class);
}
}
class Product extends Model
{
use HasFactory;
protected $table = "products";
public function category() {
return $this->belongsTo(Category::class, 'category_id');
}
}
-Название категории
---Товар 1
---Товар 2
---Товар 3
-Название категории 2
---Товар 4
---Товар 5
---Товар 6
$column = [
'id',
'name',
'path',
'image',
'slug',
];
$columnProduct = [
'id',
'name',
'path',
'image',
'slug',
];
$categories = Category::select($column)
->with(['products' => function($query) use ($columnProduct, $shopId) {
$query->select($columnProduct);
$query->where('shop_id', $shopId);
}])
->where('is_published', 1)
->get();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question