Merge pull request #690 from jitendra-webkul/jitendra

Added api for single lavel child categories
This commit is contained in:
Jitendra Singh 2019-03-14 11:34:00 +05:30 committed by GitHub
commit f28dedbe9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 3 deletions

View File

@ -0,0 +1,47 @@
<?php
namespace Webkul\API\Http\Controllers\Shop;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Webkul\Category\Repositories\CategoryRepository;
use Webkul\API\Http\Resources\Catalog\Category as CategoryResource;
/**
* Category controller
*
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CategoryController extends Controller
{
/**
* CategoryRepository object
*
* @var array
*/
protected $categoryRepository;
/**
* Create a new controller instance.
*
* @param Webkul\Category\Repositories\CategoryRepository $categoryRepository
* @return void
*/
public function __construct(CategoryRepository $categoryRepository)
{
$this->categoryRepository = $categoryRepository;
}
/**
* Returns a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return CategoryResource::collection(
$this->categoryRepository->getVisibleCategoryTree(request()->input('parent_id'))
);
}
}

View File

@ -48,13 +48,19 @@ class ResourceController extends Controller
*/
public function index()
{
$results = $this->repository->scopeQuery(function($query) {
foreach (request()->except(['page', 'limit']) as $input => $value) {
$query = $this->repository->scopeQuery(function($query) {
foreach (request()->except(['page', 'limit', 'pagination', 'sort', 'order']) as $input => $value) {
$query = $query->where($input, $value);
}
return $query;
})->paginate(request()->input('limit') ?? 10);
});
if (! is_null(request()->input('pagination')) && request()->input('pagination')) {
$results = $query->paginate(request()->input('limit') ?? 10);
} else {
$results = $query->get();
}
return $this->_config['resource']::collection($results);
}

View File

@ -18,6 +18,12 @@ class Category extends JsonResource
'id' => $this->id,
'code' => $this->code,
'name' => $this->name,
'slug' => $this->slug,
'description' => $this->description,
'meta_title' => $this->meta_title,
'meta_description' => $this->meta_description,
'meta_keywords' => $this->meta_keywords,
'status' => $this->status,
'image_url' => $this->image_url,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,

View File

@ -10,6 +10,8 @@ Route::group(['prefix' => 'api'], function ($router) {
'resource' => 'Webkul\API\Http\Resources\Catalog\Category'
]);
Route::get('descendants-categories', 'CategoryController@index');
Route::get('categories/{id}', 'ResourceController@get')->defaults('_config', [
'repository' => 'Webkul\Category\Repositories\CategoryRepository',
'resource' => 'Webkul\API\Http\Resources\Catalog\Category'