Example #1
0
 private function categorySeeder()
 {
     $c1 = Category::firstOrNew(["name" => "ข้อมูลทั่วไป", "content_type" => "content"]);
     $c1->save();
     $c2 = Category::firstOrNew(["name" => "พันธุ์ปลา", "content_type" => "fish"]);
     $c2->save();
     //relationship
     $m1 = $this->mainCategories[0];
     $c1->parent()->associate($m1)->save();
     $m2 = $this->mainCategories[1];
     $c2->parent()->associate($m2)->save();
 }
Example #2
0
 public function save(array $input)
 {
     if (isset($input['id'])) {
         $id = $input['id'];
         /* @var $category Category */
         $category = Category::find($id);
         $category->update(array_except($input, ['content_type']));
         $category->content_type = $input['content_type']['id'];
         $category->save();
         $mainCategoryId = $input['parent']['id'];
         $mainCategory = MainCategory::find($mainCategoryId);
         $category->parent()->associate($mainCategory)->save();
         return $category;
     } else {
         $parent = $input['parent'];
         $category = Category::firstOrNew(array_except($input, ['parent', 'content_type']));
         $category->content_type = $input['content_type']['id'];
         $mainCategoryId = $parent['id'];
         $mainCategory = MainCategory::find($mainCategoryId);
         $category->parent()->associate($mainCategory)->save();
         return $category;
     }
 }