بنقرة واحدة
logics-update
Guide for creating UpdateLogics in the project
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
Guide for creating UpdateLogics in the project
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
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 StoreLogics in the project
استنادا إلى تصنيف SOC المهني
| name | logics-update |
| description | Guide for creating UpdateLogics in the project |
| globs | app/Logics/**/*UpdateLogic.php |
This guide details how to create and use Logics within this project's ecosystem, specifically UpdateLogic. These components encapsulate the business logic for updating existing 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 update(UserUpdateData $data, UserUpdateLogic $logic): JsonResponse
{
// Execute the logic's run method passing the received data
return $logic->run($data);
}
}
The main execution flow of an UpdateLogic follows this order:
before(): Pre-validations and business rules. By default, it finds the model by $this->input->id.action(): Business logic execution (filling the model and saving).after(): Additional logic after the model is updated (e.g., logging changes).response(): Returning the response with HTTP 200 OK.before()Used for business rules or permissions. By default, UpdateLogic uses before() to find the model by ID. If not found, it returns a 404 error.
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;
if (! auth()->user()->can('update_users')) {
return $this->error(
message: 'You do not have permission to update users.',
status: Http::Forbidden
);
}
return parent::before(); // Always call parent::before() to ensure the model is found
}
UpdateLogic 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());
// $this->model->save();
// $this->response = collect($this->model);
return $this;
}
You can override action() if you need more complex update logic.
after()Executed after the model has been updated in the database. Useful for side effects or logging.
protected function after(): bool
{
// Logic after updating
Log::info("User {$this->model->id} has been updated.");
return true;
}
withResource()Allows transforming the updated model before sending it to the final response.
protected function withResource(): mixed
{
return new UserResource($this->model);
}
class UserUpdateLogic extends UpdateLogic
{
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
{
if (! parent::before()) {
return false;
}
// Additional checks, for example, if the user owns the record
if ($this->model->user_id !== auth()->id()) {
return $this->error('Unauthorized access to this resource', status: Http::Forbidden);
}
return true;
}
protected function withResource(): mixed
{
return new UserResource($this->model);
}
}