con un clic
logics-store
Guide for creating StoreLogics in the project
Instalar con Codex o Claude Copia este prompt, pégalo en Codex, Claude u otro asistente, y deja que revise la página de la skill y la instale por ti.
Menú
Guide for creating StoreLogics in the project
Instalar con Codex o Claude Copia este prompt, pégalo en Codex, Claude u otro asistente, y deja que revise la página de la skill y la instale por ti.
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 ShowLogics in the project
Guide for creating UpdateLogics in the project
Basado en la clasificación ocupacional SOC
| name | logics-store |
| description | Guide for creating StoreLogics in the project |
| globs | app/Logics/**/*StoreLogic.php |
This guide details how to create and use Logics within this project's ecosystem, specifically StoreLogic. These components encapsulate the business logic for creating new resources 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 store(UserStoreData $data, UserStoreLogic $logic): JsonResponse
{
// Execute the logic's run method passing the received data
return $logic->run($data);
}
}
The main execution flow of a StoreLogic follows this order:
before(): Pre-validations and business rules.action(): Business logic execution (filling the model and saving).after(): Additional logic after the model is saved (e.g., sending emails).response(): Returning the response with HTTP 201 Created.before()Used for business rules or permissions. If it returns false, execution stops and an error is returned. Use the error() method inherited from CoreLogic.
protected function before(): bool
{
if (! auth()->user()->can('create_users')) {
return $this->error(
message: 'You do not have permission to create users.',
status: Http::Forbidden
);
}
return true; // Continue execution
}
StoreLogic provides a default action() implementation that fills the model with input data and saves it.
public function action(): self
{
// Default implementation:
// $this->model->fill($this->input->toArray())->save();
// $this->response = collect($this->model->toArray());
return $this;
}
You can override action() if you need more complex creation logic.
after()Executed after the model has been saved to the database. Useful for side effects.
protected function after(): bool
{
// Logic after saving, e.g., assigning default roles
$this->model->assignRole('user');
return true;
}
withResource()Allows transforming the created model before sending it to the final response.
protected function withResource(): mixed
{
return new UserResource($this->model);
}
class UserStoreLogic extends StoreLogic
{
public function __construct(User $model)
{
parent::__construct($model);
}
public function run(Data $input): JsonResponse
{
// The base class logic() method handles the lifecycle
return $this->logic($input);
}
protected function before(): bool
{
// Check for specific conditions
if (User::where('email', $this->input->email)->exists()) {
return $this->error('Email already exists');
}
return true;
}
protected function withResource(): mixed
{
return new UserResource($this->model);
}
}