Answer the question
In order to leave comments, you need to log in
How to delete in Laravel repository?
Two models:
Category - id, title
CategoryDescription - category_id, description, slug
There is a base repository
abstract class BaseRepository implements RepositoryInterface
{
protected $model;
protected $with = [];
public function create(array $attributes): Model
{
$model = $this->model->create($attributes);
return $model;
}
public function update(int $id, array $attributes): void
{
$this->model->find($id)->update($attributes);
}
public function updateOrCreate(int $id, array $attributes): void
{
$this->model->updateOrCreate(['id' => $id], $attributes);
}
public function find(int $id): ?Model
{
return $this->model->find($id);
}
public function delete(int $id): void
{
$this->model->find($id)->delete();
}
public function with($relations)
{
if (is_string($relations)) {
$this->with = explode(',', $relations);
return $this;
}
$this->with = is_array($relations) ? $relations : [];
return $this;
}
protected function query()
{
return $this->model->newQuery()->with($this->with);
}
}
Route::prefix('category')->group(function () {
Route::get('/', [CategoryController::class, 'index']);
Route::post('/', [CategoryController::class, 'store']);
Route::prefix('{id}')->group(function () {
Route::get('/', [CategoryController::class, 'show']);
Route::put('/', [CategoryController::class, 'update']);
Route::delete('/', [CategoryController::class, 'destroy']);
Route::prefix('description')->group(function () {
Route::get('/', [CategoryDescriptionController::class, 'index']);
Route::post('/', [CategoryDescriptionController::class, 'store']);
Route::get('/{descriptionId}', [CategoryDescriptionController::class, 'show']);
Route::put('/{descriptionId}', [CategoryDescriptionController::class, 'update']);
Route::delete('/{descriptionId}', [CategoryDescriptionController::class, 'destroy']);
});
});
});
Answer the question
In order to leave comments, you need to log in
$categoryDescription->delete();
for actions related to this event, use an observer.
To delete related entities on delete cascade.
Just hide it in the depths and networks of repositories so that no one finds this Laravel shit code.
how to delete with a check in the CategoryDescriptionRepository repository to check the description id and the category to which it is attachedDo you want to check directly in the repository?)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question