/** * 设置文章分类 * * @param intger|array $categories * * @return boolean */ public function setCategories($categories) { is_array($categories) || ($categories = array($categories)); //如果不同才存 if (Input::get('old_category', '') != join(',', $categories)) { $this->termRelation()->delete(); $termRelMultiData = array_map(function ($categoryId) { return array('object_id' => $this->id, 'category_id' => $categoryId); }, $categories); //一次性写入多条 //请参考:http://www.golaravel.com/docs/4.1/queries/#inserts return false !== TermRelation::insert($termRelMultiData); } return true; }
/** * 删除分类 * * @param integer $id * * @return Response */ public function anyDelete($id) { if ($id == 1) { return Redirect::back('系统默认分类不允许删除!'); } Category::findOrFail($id)->delete(); TermRelation::where('category_id', $id)->update('category_id', 1); return Redirect::back()->withMessage("删除成功!"); }