<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
adityadarma / laravel-service-repository example snippets
protected NameService $nameService;
public function __construct(
NameService $nameService
)
{
$this->nameService = $nameService;
}
public function data()
{
$this->nameService->functionName()->getData();
}
public function json(Request $request)
{
$this->nameService->functionName()->toJson();
}
public function withResource(Request $request)
{
$this->nameService->functionName()->toJsonFromResource(ClassResource::class);
}
public function nameMethod()
{
try {
.........
if (false) {
throw new CustomException('Error exception');
}
..........
// Call toJsonFromResource at controller
return $this->setData($data)
->setMessage('Message data')
->setCode(200);
// OR
// Call toJson at controller
return $this->setData($data)
->setResource(ClassResource::class)
->setMessage('Message data')
->setCode(200);
} catch (Exception $e) {
return $this->exceptionResponse($e);
}
}
protected NameRepository $nameRepository;
public function __construct(
NameRepository $nameRepository
)
{
$this->nameRepository = $nameRepository;
}
public function data()
{
$this->nameRepository->functionName();
}