D
D
devOnMan2021-06-10 23:12:40
Laravel
devOnMan, 2021-06-10 23:12:40

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);
    }
}

there is a category repository CategoryRepository and a description repository for category CategoryDescriptionRepository

The route has the following structure
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']);
                    });
                });
            });

how to delete with a check in the CategoryDescriptionRepository repository to check the description id and the category to which it is attached

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jazzus, 2021-06-11
@devOnMan

$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.

K
Konstantin B., 2021-06-10
@Kostik_1993

how to delete with a check in the CategoryDescriptionRepository repository to check the description id and the category to which it is attached
Do you want to check directly in the repository?)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question