@NDll

Добавление в таблице в Laravel?

суть такова, есть таблица Category (id, parent, image) и таблица описания категории на разных языках CategoryDescription

при сохранение данные приходят в таком виде

5f26d2230c138170685717.png
в content_description хранится описание для разных языков
как мне делать сохранение в таблицу category_descriprions после того как произошло сохранение в categories

в сервисе пробовал так, но категория создается, а вот в описание приходит только 1 строка и пустая

вот сервис

class CategoryService
{
    private $categoryRepository;

    public function __construct(CategoryRepositoryInterface $categoryRepository)
    {
        $this->categoryRepository = $categoryRepository;
    }

    public function getById(int $id):Collection{
        return $this->categoryRepository->getById($id);
    }

    public function create(Request $request):bool {
        if (!empty($request->image)){
            $request['image'] = str_replace (env('APP_URL'),'', $request->image);
        }
        dd($request->all());
        $descriptions = $request->content_description;
        $data = $request;
        unset($data['content_description']);
        $store = $this->categoryRepository->create($data);
        if ($store){
            $storeDesc = $store->description()->attach($descriptions);
            if ($storeDesc) return true;
        }
        return false;
    }
}


репозиторий

class CategoryRepository extends BaseRepository implements CategoryRepositoryInterface
{

    protected $model;

    public function __construct($model)
    {
        $this->model = $model;
    }

    /**
     * @param int $perPage
     * @param int $languageId
     * @return mixed
     */
    public function getAllWithPaginateForAdmin(int $perPage, int $languageId){

        return $this->model
                    ->where('parent','=',0)->with(['childrens','description' => function($query) use ($languageId) {
                        $query->where('language_id', '=', $languageId);
                    }])->paginate($perPage);
    }

    public function getById(int $id): Collection
    {
        // TODO: Implement getById() method.
        return $this->model->find($id);
    }

public function create(Request $request): Model
    {
        // TODO: Implement create() method.
        $model = $this->model->create($request->all());
        $model->fresh();
        return $model;
    }
/**
     * @param int $id
     * @return Model|null
     */
    public function find(int $id): ?Model
    {
        // TODO: Implement find() method.
        return $this->model->find($id);
    }
}
  • Вопрос задан
  • 85 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы