一键导入
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 职业分类
| 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);
}
}
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