Exemplo n.º 1
0
 /**
  * Sync tag relation adding new tags as needed
  *
  * @param array $tags
  */
 public function syncTags(array $tags)
 {
     Tag::addNeededTags($tags);
     if (count($tags)) {
         $this->tags()->sync(Tag::whereIn('tag', $tags)->lists('id')->all());
         return;
     }
     $this->tags()->detach();
 }
Exemplo n.º 2
0
 /**
  * Sync tag relation adding new tags as needed
  * @param  array  $tags 
  */
 public function syncTags(array $tags)
 {
     Tag::addNeededTags($tags);
     if (count($tags)) {
         // After method sync() is complete, only the IDs in the array $tags will exist
         // in the intermediate table:post_tag_pivot
         $this->tags()->sync(Tag::whereIn('tag', $tags)->pluck('id')->all());
         return;
     }
     $this->tags()->detach();
 }
Exemplo n.º 3
0
 public function postStore($id = null)
 {
     // ---------------------------------------- HANDLE REQUEST ----------------------------------------
     // handle id
     if (!is_null($id)) {
         $data = $this->model->findorfail($id);
     } else {
         $data = $this->model->newInstance();
     }
     // ---------------------------------------- CHECK TAG ----------------------------------------
     $tags_in_db = \App\Tag::whereIn('tag', Input::get('tags'))->get();
     if (!$tags_in_db) {
         $tags_in_db = new Collection();
     }
     foreach (Input::get('tags') as $x) {
         if (!$tags_in_db->where('tag', $x)->first()->id) {
             $new_tag = new \App\Tag(['tag' => $x]);
             if (!$new_tag->save()) {
                 dd($new_tag->getErrors());
             }
             $tags_in_db->push($new_tag);
         }
     }
     // ---------------------------------------- HANDLE SAVE ----------------------------------------
     $input = Input::all();
     if (!empty($input['published_at'])) {
         $input['published_at'] = \Carbon\Carbon::createFromFormat('d/m/Y H:i', $input['published_at'])->format('Y-m-d H:i:s');
     } else {
         $input['published_at'] = null;
     }
     unset($input['longlat']);
     $input['tag_ids'] = $tags_in_db->pluck('id')->toArray();
     $data->fill($input);
     if ($data->save()) {
         if (!$this->save_required_images($data, $input)) {
             return redirect()->back()->withInput()->withErrors($data->getErrors());
         }
         return redirect()->route('admin.' . $this->view_name . '.show', ['id' => $data->id])->with('alert_success', '"' . $data->{$data->getNameField()} . '" has been saved successfully');
     } else {
         return redirect()->back()->withInput()->withErrors($data->getErrors());
     }
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request)
 {
     $validator = Validator::make($request->all(), ['key' => 'required|size:128', 'email' => 'email|required|max:254']);
     $redirectToEditUrl = function () use($request) {
         return Redirect::to('/subscription/edit?email=' . $request->input('email') . '&key=' . $request->input('key'));
     };
     if ($validator->fails()) {
         $redirectToEditUrl();
     }
     $subscription = Subscription::with('topics')->where('email', $request->input('email'))->where('key', $request->input('key'))->firstOrFail();
     $subscription->topics()->detach();
     if (!empty($request->input('topics')) && is_array($request->input('topics'))) {
         $tags = Tag::whereIn('id', array_keys($request->input('topics')))->get();
         if (count($tags) != count(array_keys($request->input('topics')))) {
             Session::flash('errors', 'Woops. Something went badly wrong.');
             return $redirectToEditUrl();
         }
         $subscription->topics()->saveMany($tags->all());
     }
     $subscription->save();
     Session::flash('status', 'Saved.');
     return $redirectToEditUrl();
 }
Exemplo n.º 5
0
 public function deltag(Request $request)
 {
     $id = (array) $request->id;
     if (Tag::whereIn('id', $id)->delete()) {
         return response()->json(200);
     } else {
         return response()->json(404);
     }
 }
Exemplo n.º 6
0
 public function restoreTags(Article $article)
 {
     $tagIds = $article->tags->lists('id');
     Tag::whereIn('id', $tagIds)->increment('count');
 }
Exemplo n.º 7
0
 /**
  * Sync Tag với tiện ích thêm mới nếu Tag chưa tồn tại
  *
  * @param array $tags
  * @return array
  */
 public function syncTags($tags)
 {
     $allTagsFound = Tag::whereIn('id', $tags)->lists('id')->toArray();
     // Mảng Sync cần loại giá trị tag chưa tồn tại
     $tagsSync = array_intersect($tags, $allTagsFound);
     // Sau đó nếu tag chưa tồn tại, cần thêm mới tag vào db, và trả lại id tương ứng để đưa vào mảng Sync
     foreach (array_diff($tags, $allTagsFound) as $tag) {
         $tagsSync[] = Tag::create(['name' => $tag])->id;
     }
     return $this->tags()->sync($tagsSync);
 }