원클릭으로
logics-show
Guide for creating ShowLogics in the project
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
메뉴
Guide for creating ShowLogics in the project
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
SOC 직업 분류 기준
Guide for creating DeleteLogics in the project
Guide for creating API endpoints and routing using Logics
Guide for creating IndexLogics in the project
Guide for creating StoreLogics in the project
Guide for creating UpdateLogics in the project
| name | logics-show |
| description | Guide for creating ShowLogics in the project |
| globs | app/Logics/**/*ShowLogic.php |
This guide details how to create and use Logics within this project's ecosystem, specifically ShowLogic. These components encapsulate the business logic for retrieving a single resource and are used in controllers via dependency injection.
Logics are injected directly into controller methods along with Data Objects.
class UserController extends Controller
{
public function show(UserShowData $data, UserShowLogic $logic): JsonResponse
{
// Execute the logic's run method passing the received data
return $logic->run($data);
}
}
The main execution flow of a ShowLogic follows this order:
before(): Pre-validations and finding the model by ID (default behavior).action(): Business logic execution (querying the model).after(): Additional logic after the record is retrieved.response(): Returning the response with the resource or HTTP 404 Not Found.before()By default, ShowLogic tries to find the model using $this->input->id. If not found, it returns an error with status code 404.
protected function before(): bool
{
// Default implementation:
// $foundModel = $this->model->find($this->input->id);
// if (is_null($foundModel)) {
// return $this->error(message: 'Not Found', status: Http::NotFound);
// }
// $this->model = $foundModel;
// You can add extra validations here
if (! auth()->user()->can('view', $this->model)) {
return $this->error('Unauthorized', status: Http::Forbidden);
}
return true;
}
makeQuery()You can customize how the record is retrieved by overriding makeQuery(). By default, it looks for a scopeShow() or show() method on the model, or uses where('id', $this->input->id).
protected function makeQuery(): Builder
{
// Return a Builder with eager loading or specific conditions
return $this->model->newQuery()->with('roles')->where('is_active', true);
}
after()Executed after the query has been processed.
protected function after(): bool
{
// Additional logic, e.g., logging
Log::info("Viewed record: " . $this->model->id);
return true;
}
withResource()Allows transforming the found model before sending it to the final response.
protected function withResource(): mixed
{
return new UserResource($this->model);
}
class UserShowLogic extends ShowLogic
{
public function __construct(User $model)
{
parent::__construct($model);
}
public function run(Data $input): JsonResponse|StreamedResponse
{
// The base class logic() method handles the lifecycle
return $this->logic($input);
}
protected function before(): bool
{
if (! parent::before()) return false;
return auth()->user()->can('view', $this->model);
}
protected function makeQuery(): Builder
{
return parent::makeQuery()->with(['roles', 'profile']);
}
protected function withResource(): mixed
{
return new UserResource($this->model);
}
}