File: /home/armgroup/libs/app/Http/Controllers/Frontend/IndexController.php
<?php
namespace App\Http\Controllers\Frontend;
use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\Banner;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Product;
use Artesaos\SEOTools\Facades\SEOTools;
use Illuminate\Support\Facades\Storage;
class IndexController extends Controller
{
public function index()
{
SEOTools::setTitle(settings('title'));
SEOTools::setDescription(settings('description'));
$banners = Banner::getBanners([
'main-slider',
'top-of-latest-products',
'why-us',
'top-of-footer',
]);
[
$slider,
] = [
$banners->firstWhere('position', 'main-slider'),
];
$indexCategories = Category::query()
->where('type', Product::class)
->where('status', true)
->where('show_in_index', true)
->whereNotNull('image')
->limit(8)
->get(['name', 'slug', 'image']);
$articles = Article::query()
->with('categories:id,name')
->where('status', true)
->whereNotNull('image')
->latest('id')
->limit(4)
->get(['title', 'slug', 'image', 'description', 'created_at']);
$brands = Brand::query()
->whereNotNull('image')
->latest('id')
// ->limit(12)
->get(['name', 'slug', 'image', 'created_at']);
$specialProducts = Product::query()
->with(['options', 'activeOptionValues'])
->where('status', true)
->whereNotNull('special')
->where('special_started_at', '<=', now())
->where('special_ended_at', '>=', now())
->limit(12)
->get(['id', 'name', 'slug', 'image', 'code', 'price', 'stock', 'special', 'special_started_at', 'special_ended_at', 'country_code']);
$latestProducts = Product::query()
->with(['options', 'activeOptionValues'])
->where('status', true)
->limit(12)
->get(['id', 'name', 'slug', 'image', 'code', 'price', 'stock', 'special', 'special_started_at', 'special_ended_at', 'country_code']);
$sellerProducts = Product::query()
->where('status', true)
->where('best_seller', true)
->get(['id', 'name', 'slug', 'image', 'code', 'price', 'stock', 'special', 'special_started_at', 'special_ended_at', 'country_code']);
return view('frontend.index',
compact(
'slider',
'brands',
'articles',
'specialProducts',
'latestProducts',
'sellerProducts',
'indexCategories'
));
}
public function getFileSrc($model, $name = null)
{
$model = 'App\\Models\\'.$model;
return $this->checkAccess($name, $model) ? response()->file(Storage::path($name)) : abort(404);
}
private function checkAccess($name, string $model): bool
{
// for users check user_id with folder name and for admins check permission
return $name && class_exists($model) && (auth()->id() == explode('/',
$name)[$model::USER_ID_SEGMENT] || auth()->user()->can($model::PERMISSION_NAME));
}
}