public static function boot() { parent::boot(); static::creating(function ($term) { }); static::updating(function ($term) { }); static::deleting(function ($term) { if ($term->type == 'post_category') { // set to uncategorized $postTerms = \App\PostTerm::where('term_id', $term->id)->update(['term_id' => 6]); } }); static::created(function ($term) { }); static::updated(function ($term) { }); static::deleted(function ($term) { }); }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // $validator = \Validator::make($request->all(), ['title' => 'required']); if ($validator->fails()) { return response()->json(array('status' => 500, 'monolog' => array('title' => 'errors', 'message' => implode($validator->errors()->all(), '<br>')))); } DB::beginTransaction(); $post = Post::find($id); $post->user_id = \Auth::user()->get()->id; $post->slug = $request->slug['en']; $post->route = str_replace('-', '_', $request->slug['en']); $post->status = $request->status; $post->save(); // delete first $post->postLocales()->delete(); // locale foreach ($request->title as $key => $value) { $locale = new \App\postLocale(); $locale->post_id = $post->id; $locale->title = $request->title[$key]; $locale->content = $request->content[$key]; $locale->meta_keyword = $request->meta_keyword[$key]; $locale->meta_description = $request->meta_description[$key]; $locale->slug = $locale->slug($request->slug[$key], $post->id); $locale->locale = $key; $locale->save(); } // category $term = $post->terms()->where('type', 'post_category')->first(); $postTerm = \App\PostTerm::where('post_id', $post->id)->where('term_id', $term->id)->first(); $postTerm->term_id = $request->term_id; $postTerm->post_id = $post->id; $postTerm->save(); DB::commit(); return response()->json(array('status' => 200, 'monolog' => array('title' => 'success', 'message' => 'object has been updated'))); }