File: /home/armgroup/libs/app/Http/Controllers/Frontend/ProductController.php
<?php
namespace App\Http\Controllers\Frontend;
use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\Product;
use Artesaos\SEOTools\Facades\SEOTools;
use Illuminate\Database\Eloquent\Builder;
class ProductController extends Controller
{
public function show(Product $product)
{
abort_unless(checkStatusAndPermission($product, 'manage products'), 404);
SEOTools::setTitle($product->meta_title ?: $product->name);
SEOTools::setDescription($product->short_description);
SEOTools::addImages([asset($product->image)]);
$product->load([
'images',
'attributes',
'attributes.group',
'values',
'brand',
'scores',
'tags',
'approvedComments',
'categories',
'categories.parent',
]);
$product->loadCount('approvedComments');
$groupedAttributes = $product->attributes->groupBy('group_id');
$product_category = $product->categories()->where('is_main', true)->first();
$relatedProducts = Product::query()
->where('status', true)
->where('id', '<>', $product->id)
->where('suggest', false)
->whereRelation('categories', 'id', $product_category?->id)
->take(10)
->get(['id', 'name', 'latin_name', 'slug', 'image', 'price', 'stock', 'special', 'special_started_at', 'special_ended_at', 'country_code', 'code']);
$suggestProducts = Product::query()
->where('status', true)
->where('id', '<>', $product->id)
->where('suggest', true)
->whereRelation('categories', 'id', $product_category?->id)
->take(10)
->get(['id', 'name', 'latin_name', 'slug', 'image', 'price', 'stock', 'special', 'special_started_at', 'special_ended_at', 'country_code', 'code']);
$relatedArticles = Article::query()
->when($product->categories->first(), function ($query) use ($product) {
$query->whereHas('productCategories', function (Builder $query) use ($product) {
$query->whereIn('category_id', $product->categories->first()->parents->pluck('id'));
});
})
->where('status', true)
->whereNotNull('image')
->limit(12)
->get(['title', 'slug', 'image', 'description']);
$highlightAttributes = $product->attributes->where('highlight', true);
if ($highlightAttributes) {
$highlightAttributes = $highlightAttributes->chunk(ceil(count($highlightAttributes) / 2));
}
return view('frontend.products.show',
compact(
'product',
'relatedArticles',
'groupedAttributes',
'highlightAttributes',
'relatedProducts',
'suggestProducts',
'product_category'
));
}
}