Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

iluminar/goodwork

2224 stars
2 code files
View iluminar/goodwork on GitHub

app/Base/Repositories/CategoryRepository.php

Open in GitHub
use App\Base\Models\Category;
 
class CategoryRepository
{
protected $model;
 
public function __construct(Category $category)
{
$this->model = $category;
}
 
public function getAllCategories()
{
return $this->model->get();
}
 
public function create(array $attributes)
{
return $this->model->create($attributes);
}
}

app/Base/Http/Controllers/CategoryController.php

Open in GitHub
use App\Base\Repositories\CategoryRepository;
use App\Base\Http\Requests\CategoryStoreRequest;
 
class CategoryController extends Controller
{
private $repository;
 
public function __construct(CategoryRepository $repository)
{
$this->repository = $repository;
}
 
public function index()
{
$categories = $this->repository->getAllCategories();
 
return response()->json([
'status' => 'success',
'categories' => $categories,
]);
}
 
public function store(CategoryStoreRequest $request)
{
$category = $this->repository->create($request->all());
 
return $this->successResponse(
'misc.New category has been created',
'category',
$category,
201
);
}
}