コード例 #1
0
 /**
  * Create new spec
  *
  * @param $category
  * @param $name
  * @param $descriptions
  * @param $image
  * @return mixed
  */
 public function create($category, $name, $descriptions, $image)
 {
     $category = Category::findOrFail($category);
     $spec = null;
     try {
         $spec = new Spec();
         $spec->category_id = $category->id;
         $spec->name = $name;
         $spec->save();
         //cria as traduções
         foreach ($descriptions as $iso => $value) {
             $lang = Lang::whereIso($iso)->first();
             $this->translationRepositoryInterface->create($lang->id, 'specs', $spec->id, $value);
         }
         //atualiza as imagens
         if (!is_null($image)) {
             $this->mediaRepositoryInterface->updateTable($image, 'specs', $descriptions->id);
         }
     } catch (\Exception $e) {
         if (!is_null($spec) && $spec->exists) {
             $this->delete($spec->id);
         }
         return false;
     }
     return true;
 }
コード例 #2
0
 /**
  * Create a new category
  *
  * @param $type
  * @param $names
  * @param $parent_id
  * @return mixed
  */
 public function create($type, $names, $parent_id)
 {
     //procurar todas as categorias com o mesmo parent_id e o mesmo tipo
     $categories = Category::where('type', $type)->where('parent_id', $parent_id)->get();
     if (!$categories->isEmpty()) {
         //se o resultado não for null
         foreach ($categories as $category) {
             $translations = $this->translationRepositoryInterface->get('categories', $category->id);
             foreach ($translations as $translation) {
                 //precorrer a lista de categorias e verificar a tradução de cada uma delas
                 //se a tradução for igual para uma delas PARA
                 if (in_array($translation->value, $names)) {
                     return false;
                 }
             }
         }
     }
     //se a tradução for diferente para todas adiciona-se a categoria e a tradução
     //cria a categoria
     $category = null;
     try {
         $category = new Category();
         $category->type = $type;
         $category->parent_id = $parent_id;
         $category->save();
         //cria as traduções
         foreach ($names as $iso => $name) {
             $lang = Lang::whereIso($iso)->first();
             $this->translationRepositoryInterface->create($lang->id, 'categories', $category->id, strtolower($name));
         }
     } catch (\Exception $e) {
         if (!is_null($category) && $category->exists) {
             $this->delete($category->id);
         }
         return false;
     }
     return true;
 }