sarga/packages/Sarga/API/Http/Controllers/Brands.php

86 lines
2.4 KiB
PHP
Raw Normal View History

2022-05-04 12:09:11 +00:00
<?php
namespace Sarga\API\Http\Controllers;
use Illuminate\Http\Request;
2022-12-21 14:33:00 +00:00
use Illuminate\Support\Facades\Log;
2022-05-04 12:09:11 +00:00
use Sarga\API\Http\Resources\Catalog\Brand;
use Sarga\Brand\Repositories\BrandRepository;
class Brands extends \Webkul\RestApi\Http\Controllers\V1\Shop\ResourceController
{
2022-11-02 11:24:26 +00:00
protected $requestException = ['page', 'limit', 'pagination', 'sort', 'order', 'token','locale','search','category','menu'];
2022-05-04 12:09:11 +00:00
/**
* Is resource authorized.
*
* @return bool
*/
public function isAuthorized()
{
return false;
}
/**
* Repository class name.
*
* @return string
*/
public function repository()
{
return BrandRepository::class;
}
/**
* Resource class name.
*
* @return string
*/
public function resource()
{
return Brand::class;
}
public function allResources(Request $request)
{
$query = $this->getRepositoryInstance()->scopeQuery(function ($query) use ($request) {
foreach ($request->except($this->requestException) as $input => $value) {
$query = $query->whereIn($input, array_map('trim', explode(',', $value)));
}
2022-11-02 11:24:26 +00:00
if($menu = $request->input('menu')){
$query = $query->join('menu_brands','brands.id','=','menu_brands.brand_id')
->where('menu_brands.menu_id', $menu);
2022-05-04 12:09:11 +00:00
}
2022-11-02 11:24:26 +00:00
elseif($category = $request->input('category'))
{
$query = $query->join('category_brands','brands.id','=','category_brands.brand_id')
2022-05-04 12:09:11 +00:00
->where('category_brands.category_id',$category);
}
2022-11-02 11:24:26 +00:00
if($key = $request->input('search')){
$query = $query->where('name','like', '%'.$key.'%');
}
2022-05-04 12:09:11 +00:00
if ($sort = $request->input('sort')) {
2022-12-17 10:00:11 +00:00
return $query->orderBy($sort, $request->input('order') ?? 'asc');
2022-05-04 12:09:11 +00:00
}
2022-12-21 13:59:26 +00:00
return $query->withCount('flats')->having('flats_count', '>', 1)
->orderBy('position', 'desc')
->orderBy('name', 'asc');
2022-12-21 08:10:37 +00:00
});
2022-05-04 12:09:11 +00:00
2022-12-21 10:14:15 +00:00
2022-05-04 12:09:11 +00:00
if (is_null($request->input('pagination')) || $request->input('pagination')) {
$results = $query->paginate($request->input('limit') ?? 10);
} else {
$results = $query->get();
2022-12-21 14:33:00 +00:00
Log::info($results);
2022-05-04 12:09:11 +00:00
}
return $this->getResourceCollection($results);
}
}