Skip to content

Instantly share code, notes, and snippets.

@jalallinux
Created October 8, 2022 12:47
Show Gist options
  • Save jalallinux/c768fbc62a86e940b1e3d0a2572fa48e to your computer and use it in GitHub Desktop.
Save jalallinux/c768fbc62a86e940b1e3d0a2572fa48e to your computer and use it in GitHub Desktop.
Laravel - Controller Model API Stub
<?php
namespace {{ namespace }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\JsonResponse;
use App\Repository\Contracts\{{ model }}Contract;
/**
* {{ model }} management controller
* @author
*/
class {{ class }} extends Controller
{
/**
* Set {{ model }}Controller repository class
* @return string
* @author
*/
protected function repositoryClass()
{
// Define repository class of this controller.
// Remove method if controller doesn't have any repository class;
return {{ model }}Contract::class;
}
/**
* Display a listing of the {{ modelVariable }}.
*
* @param {{ model }}IndexRequest $request
* @return AnonymousResourceCollection
* @author
*/
public function index({{ model }}IndexRequest $request)
{
$list = $this->repository->paginate($request->criteria(), $request->query('perPage'));
return {{ model }}Resource::collection($list);
}
/**
* Display the specified {{ modelVariable }}.
*
* @param {{ model }}ShowRequest $request
* @return {{ model }}Resource
* @author
*/
public function show({{ model }}ShowRequest $request)
{
${{ modelVariable }} = $this->repository->findBy('uuid', $request->safe()->{{ modelVariable }});
return new {{ model }}Resource(${{ modelVariable }});
}
/**
* Store a newly created {{ modelVariable }} in database.
*
* @param {{ model }}StoreRequest $request
* @return {{ model }}Resource
* @throws \Throwable
* @author
*/
public function store({{ model }}StoreRequest $request)
{
${{ modelVariable }} = $this->repository->create($request->safe()->all());
return new {{ model }}Resource(${{ modelVariable }});
}
/**
* Update the specified {{ modelVariable }} in database.
*
* @param {{ model }}UpdateRequest $request
* @return {{ model }}Resource
* @author
*/
public function update({{ model }}UpdateRequest $request)
{
${{ modelVariable }} = $this->repository->updateBy('uuid', $request->safe()->{{ modelVariable }}, $request->safe()->except('{{ modelVariable }}'));
return new {{ model }}Resource(${{ modelVariable }});
}
/**
* Remove the specified {{ modelVariable }} from database.
*
* @param {{ model }}DestroyRequest $request
* @return JsonResponse
* @author
*/
public function destroy({{ model }}DestroyRequest $request)
{
try {
if ($this->repository->deleteBy('uuid', $request->safe()->{{ modelVariable }})) {
return responseMessage(__("response.crud.destroy.success", [
'attribute' => __('validation.attributes.{{ modelVariable }}')
]));
}
} catch (\Exception $exception) {}
return responseMessage(__('response.crud.destroy.failure', [
'attribute' => __('validation.attributes.{{ modelVariable }}')
]), 403);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment