N
N
NDll2020-08-02 17:52:15
Laravel
NDll, 2020-08-02 17:52:15

Adding to a table in Laravel?

the essence is this, there is a Category (id, parent, image) table and a category description table in different languages ​​​​CategoryDescription

when saving data comes in this form

5f26d2230c138170685717.png
in content_description, a description is stored for different languages ​​\u200b\u200bhow
do I save to the category_descriprions table after saving in categories

in service tried like this, but the category is created, but only 1 line comes to the description and

the service is empty

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


repository

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

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question